htmlEscape vs goog.string.htmlEscape
JavaScript performance comparison
Preparation code
<script>
Benchmark.prototype.setup = function() {
var _rhtml = /[&<>"]/g
var _rspecial = {
'&':'&',
'<':'<',
'>':'>',
'"':'"'
};
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_, '&');
}
if (str.indexOf('<') != -1) {
str = str.replace(goog_string_ltRe_, '<');
}
if (str.indexOf('>') != -1) {
str = str.replace(goog_string_gtRe_, '>');
}
if (str.indexOf('"') != -1) {
str = str.replace(goog_string_quotRe_, '"');
}
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_, '&');
}
if (str.indexOf('<') != -1) {
str = str.replace(goog_string_ltRe_, '<');
}
if (str.indexOf('>') != -1) {
str = str.replace(goog_string_gtRe_, ' >');
}
if (str.indexOf('"') != -1) {
str = str.replace(goog_string_quotRe_, '"');
}
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_, '&');
}
if (~str.indexOf('<')) {
str = str.replace(goog_string_ltRe_, '<');
}
if (~str.indexOf('>')) {
str = str.replace(goog_string_gtRe_, ' >');
}
if (~str.indexOf('"')) {
str = str.replace(goog_string_quotRe_, '"');
}
return str;
}
};
</script>
Test runner
Warning! For accurate results, please disable Firebug before running the tests. (Why?)
Java applet disabled.
| Test | Ops/sec | |
|---|---|---|
htmlEscape |
|
pending… |
goog.string.htmlEscape |
|
pending… |
htmlEscape + typeOf |
|
pending… |
goog.string.htmlEscape + typeOf |
|
pending… |
goog.string.htmlEscape + typeOf + unary bitwise complement |
|
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:
- Revision 1: published by RubaXa
- Revision 2: published by termi
0 comments