bem / bem-xjst

bem-xjst (eXtensible JavaScript Templates): declarative template engine for the browser and server
https://bem.github.io/bem-xjst
Other
116 stars 48 forks source link

Possible optimization of xmlEscape #252

Closed qfox closed 8 years ago

qfox commented 8 years ago

In lodash there is simple check before replacing:

var reUnescapedHtml = /[&<>"'`]/g,
    reHasUnescapedHtml = RegExp(reUnescapedHtml.source);

    function escape(string) {
      string = toString(string);
      return (string && reHasUnescapedHtml.test(string))
        ? string.replace(reUnescapedHtml, escapeHtmlChar)
        : string;
    }

Guess we should try something like that:

var hasEntities = /[&<>]/;

exports.xmlEscape = function(str) {
  str = str + '';
  hasEntities.test(str) && (str = str
    .replace(/&/g, '&amp;')
    .replace(/</g, '&lt;')
    .replace(/>/g, '&gt;'));
  return str;
};
miripiruni commented 8 years ago

So, where is the PR? ;)

miripiruni commented 8 years ago

We need benchmark result.

miripiruni commented 8 years ago

We don’t use RegExp after https://github.com/bem/bem-xjst/pull/328