Escape Html special chars

JavaScript performance comparison

Revision 13 of this test case created by hamidfarahanialavi

Preparation code

  
      
<script>
Benchmark.prototype.setup = function() {
  var specialchars = "&;\'><\""; var escapeMap = { "&&quot;: "&", "<": "<", ">": ">", '"': '"', "'": ''' }; function escapeHTML(string) { return String(string).replace(/&(?!\w+;)|[<>"']/g, function (s) { return escapeMap[s] || s; }); } function HtmlEncode(s) { var el = document.createElement("div"); el.innerText = el.textContent = s; s = el.innerHTML; return s; } var BS = /\\/g, CR = /\r/g, LS = /\u2028/g, PS = /\u2029/g, NL = /\n/g, LF = /\f/g, SQ = /'/g, DQ = /"/g, TB = /\t/g; var escapeJs = function(s) { if (typeof s === "string") { return s .replace(BS, '\\\\') .replace(DQ, '\\"') .replace(SQ, "\\'") .replace(CR, '\\r') .replace(LS, '\\u2028') .replace(PS, '\\u2029') .replace(NL, '\\n') .replace(LF, '\\f') .replace(TB, "\\t"); } return s; }; var escape = function(string) { return (''+string).replace(/&/g, '&').replace(/</g, '<').rep

};

Benchmark.prototype.teardown = function() {
    

};
</script>

Preparation code output

Test runner

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

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
mustache.js
escapeHTML(specialchars);
pending…
CLAIM FORM
// async test
HtmlEncode(specialchars);
pending…
dust.js
// async test
escapeJs(specialchars);
pending…
underscore.js
// async test
escape(specialchars);
pending…
mod
// async test
escapeHtml2(specialchars);
pending…
mod3
// async test
escapeHtml3(specialchars);
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.

0 Comments