htmlEscape vs goog.string.htmlEscape

JavaScript performance comparison

Test case created by RubaXa

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var _rhtml = /[&<>"]/g
    var _rspecial = {
        '&':'&amp;',
        '<':'&lt;',
        '>':'&gt;',
        '"':'&quot;'
    };
   
    function _replaceEntity(a) {
        return _rspecial[a];
    }
   
   
    function htmlEscape(str) {
        if (_rhtml.test(str)) {
                return str.replace(_rhtml, _replaceEntity);
        }
        return str;
    }
   
    function htmlEscape_typeOf(str) {
        if (typeof str === "string") {
                if (_rhtml.test(str)) {
                        return str.replace(_rhtml, _replaceEntity);
                }
        }
        return str;
    }
   
   
    var strings = [
        428
        , 48360487568346873894576078956789346943
        , 5.35235
        , false
        , true
        , null
        , NaN
        , {}
        , []
        , 'safj'
        , 'njsjsdghjksdhgjsdhgjahdjsklghjsklgndjcgdfsgjnhfdsjncfgjsdhcfjlghx'
        , 'basjdgh - & - jdsghjdshgj', 'basjdgh - & < - jdsghjdshgj', 'basjdgh - & <> - jdsghjdshgj'
        , 'basjdgh - & <> " - jdsghjdshgj', '& <> " - jdsghjdshgjdjsgjkah4i7y87937x84xnu <> #^%#&#^7>@<^>@#^^>*^*(><$>!^<>%$&<^$&^*#<#>^@^"!^@#^"@^$M@#>^<#@>^<@#^:<!^><!>#<^$><$!<^><$!#>^<$#>^&<>#@<&>$#*<#%^#!$<>^<:@:<^!:#<&"#%<&@$"&'
        , '"sdj ghjsagjkajskghsajk gsadhg jkshj gsjakg akshg u9482y1t846., 3y3782yzn87393a87 usgjsagahjks g " adgs h gjahsjk gskdhgjk ahsjk gksg ksagagds < 463436 4756 7w678 t678gsagas > 7a67678se6t78678gf678bv6xz78@$ ^ $# ^ * ^ & * & #$ jfgsuhgjkshjk fgsdgasgegadhksgj skagkdgj ksdh jkgksagj ksah gjkaskgksahgjk sj gjksh gjkhasjk gasg S < > sdgjasjy347 6378659 8072827687 59092768 0786 * ^ & * % ^ & % $ ^ & s gasg as., g., .46 "<> A< >$r878784786t748657 aa84678a 290756902467a07a249 64 <>A>#^ A$8a9a684289689389 98 9a3898926'
        , ' 725y832y8924 89q2q49 8978w7ysdjvcvmxbvasugjwakehgsnzjkg ksfg sa"s adgjadhsu gjkdsag sdajk g " sadgjaksguasgiwguiywigsua giqwuirghuiguwy7834t78re78da'
    ];
   
    var goog_string_amperRe_ = /&/g;
    var goog_string_ltRe_ = /</g;
    var goog_string_gtRe_ = />/g;
    var goog_string_quotRe_ = /\"/g;
    var goog_string_allRe_ = /[&<>\"]/;
   
    function goog_string_htmlEscape(str) {
        // quick test helps in the case when there are no chars to replace, in
        // worst case this makes barely a difference to the time taken
        if (!goog_string_allRe_.test(str)) return str;
   
        // str.indexOf is faster than regex.test in this case
        if (str.indexOf('&') != -1) {
                str = str.replace(goog_string_amperRe_, '&amp;');
        }
        if (str.indexOf('<') != -1) {
                str = str.replace(goog_string_ltRe_, '&lt;');
        }
        if (str.indexOf('>') != -1) {
                str = str.replace(goog_string_gtRe_, '&gt;');
        }
        if (str.indexOf('"') != -1) {
                str = str.replace(goog_string_quotRe_, '&quot;');
        }
        return str;
    }
    ;
   
   
    function goog_string_htmlEscape_typeOf(str) {
        // quick test helps in the case when there are no chars to replace, in
        // worst case this makes barely a difference to the time taken
        if (typeof str !== 'string' || !goog_string_allRe_.test(str)) return str;
   
        // str.indexOf is faster than regex.test in this case
        if (str.indexOf('&') != -1) {
                str = str.replace(goog_string_amperRe_, '&amp;');
        }
        if (str.indexOf('<') != -1) {
                str = str.replace(goog_string_ltRe_, '&lt;');
        }
        if (str.indexOf('>') != -1) {
                str = str.replace(goog_string_gtRe_, ' &gt;');
        }
        if (str.indexOf('"') != -1) {
                str = str.replace(goog_string_quotRe_, '&quot;');
        }
        return str;
    }
   
   
    function goog_string_htmlEscape_typeOf_unary(str) {
        // quick test helps in the case when there are no chars to replace, in
        // worst case this makes barely a difference to the time taken
        if (typeof str !== 'string' || !goog_string_allRe_.test(str)) return str;
   
        // str.indexOf is faster than regex.test in this case
        if (~str.indexOf('&')) {
                str = str.replace(goog_string_amperRe_, '&amp;');
        }
        if (~str.indexOf('<')) {
                str = str.replace(goog_string_ltRe_, '&lt;');
        }
        if (~str.indexOf('>')) {
                str = str.replace(goog_string_gtRe_, ' &gt;');
        }
        if (~str.indexOf('"')) {
                str = str.replace(goog_string_quotRe_, '&quot;');
        }
        return str;
    }
};
</script>

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
htmlEscape
for (var i = 0, n = strings.length; i < n; i += 1) {
  htmlEscape(strings[i]);
}
pending…
goog.string.htmlEscape
for (var i = 0, n = strings.length; i < n; i += 1) {
  goog_string_htmlEscape(strings[i]);
}
pending…
htmlEscape + typeOf
for (var i = 0, n = strings.length; i < n; i += 1) {
  htmlEscape_typeOf(strings[i]);
}
pending…
goog.string.htmlEscape + typeOf
for (var i = 0, n = strings.length; i < n; i += 1) {
  goog_string_htmlEscape_typeOf(strings[i]);
}
pending…
goog.string.htmlEscape + typeOf + unary bitwise complement
for (var i = 0, n = strings.length; i < n; i += 1) {
  goog_string_htmlEscape_typeOf_unary(strings[i]);
}
pending…

Compare results of other browsers

Revisions

You can edit these tests or add even more tests to this page by appending /edit to the URL. Here’s a list of current revisions for this page:

0 comments

Add a comment