Editing htmlEscape vs goog.string.htmlEscape This edit will create a new revision. Your details (optional) Name Email (won’t be displayed; might be used for Gravatar) URL Test case details Title * Published (uncheck if you want to fiddle around before making the page public) Description (in case you feel further explanation is needed)(Markdown syntax is allowed) Are you a spammer? (just answer the question) Preparation code Preparation code HTML (this will be inserted in the <body> of a valid HTML5 document in standards mode) (useful when testing DOM operations or including libraries) Include JavaScript libraries as follows: <script src="//cdn.ext/library.js"></script> Define setup for all tests (variables, functions, arrays or other objects that will be used in the tests) (runs before each clocked test loop, outside of the timed code region) (e.g. define local test variables, reset global variables, clear canvas, etc.) (see FAQ) 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; } Define teardown for all tests (runs after each clocked test loop, outside of the timed code region) (see FAQ) Code snippets to compare Test 1 Title Async (check if this is an asynchronous test) Code for (var i = 0, n = strings.length; i < n; i += 1) { htmlEscape(strings[i]); } Test 2 Title Async (check if this is an asynchronous test) Code for (var i = 0, n = strings.length; i < n; i += 1) { goog_string_htmlEscape(strings[i]); } Test 3 Title Async (check if this is an asynchronous test) Code for (var i = 0, n = strings.length; i < n; i += 1) { htmlEscape_typeOf(strings[i]); } Test 4 Title Async (check if this is an asynchronous test) Code for (var i = 0, n = strings.length; i < n; i += 1) { goog_string_htmlEscape_typeOf(strings[i]); } Test 5 Title Async (check if this is an asynchronous test) Code for (var i = 0, n = strings.length; i < n; i += 1) { goog_string_htmlEscape_typeOf_unary(strings[i]); }