htmlEncodeStringReplacement

JavaScript performance comparison

Revision 31 of this test case created by Marek

Preparation code

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>

      
<script>
Benchmark.prototype.setup = function() {
  var html = document.body.innerHTML;
  var map = {
    "&": "&amp;",
    "'": "&#39;",
    '"': "&quot;",
    "<": "&lt;",
    ">": "&gt;"
  };
  
  function replaceEntity(chr) {
    return map[chr];
  }
  var tempElement = $(document.createElement("div"));
  String.prototype.replaceAll = function(str1, str2, ignore) {
    return this.replace(new RegExp(str1.replace(/([\/\,\!\\\^\$\{\}\[\]\(\)\.\*\+\?\|\<\>\-\&])/g, "\\$&"), (ignore ? "gi" : "g")), (typeof(str2) == "string") ? str2.replace(/\$/g, "$$$$") : str2);
  };
  String.prototype.htmlEncode = function() {
    return this.replace(/&/g, '&amp;').replace(/"/g, '&quot;').replace(/'/g, '&#39;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
  };
  var re_amp = /&/g;
  var re_squo = /'/g;
  var re_dquo = /"/g;
  var re_lt = /</g;
  var re_gt = />/g;
  
  var DOMtext = document.createTextNode("test");
  var DOMnative = document.createElement("span");
  DOMnative.appendChild(DOMtext);
  var RE_amp = new RegExp("&", "g");
  var RE_squo = new RegExp("'", "g");
  var RE_dquo = new RegExp("\"", "g");
  var RE_lt = new RegExp("<", "g");
  var RE_gt = new RegExp(">", "g");
  String.prototype.htmlSafe = function() {
    return this
      .replace(RE_amp, '&amp;')
      .replace(RE_dquo, '&quot;')
      .replace(RE_squo, '&#39;')
      .replace(RE_lt, '&lt;')
      .replace(RE_gt, '&gt;');
  };
  String.prototype.replaceAll2 = function(s1, s2) {
    return this.split(s1).join(s2);
  };

};
</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
multiple replace()
return html.replace(/&/g, '&amp;', "g").replace(/"/g, '&quot;', "g").replace(/'/g, '&#39;', "g").replace(/</g, '&lt;', "g").replace(/>/g, '&gt;', "g");
pending…
single replace with map
return html.replace(/[&"'\<\>]/g, function(c) {
  return map[c];
});
pending…
single replace with switch
return html.replace(/[&"'\<\>]/g, function(c) {
  switch (c) {
    case "&":
      return "&amp;";
    case "'":
      return "&#39;";
    case '"':
      return "&quot;";
    case "<":
      return "&lt;";
    case ">":
      return "&gt;";
  }

});
pending…
use jQuery
return $('<div/>').text(html).html();
pending…
use jQuery with pre-created element
return tempElement.text(html).html();
pending…
predefined regexp literals
return html.replace(re_amp, '&amp;').replace(re_dquo, '&quot;').replace(re_squo, '&#39;').replace(re_lt, '&lt;').replace(re_gt, '&gt;');
pending…
.innerHTML
return document.createElement('div').appendChild(document.createTextNode(html)).parentNode.innerHTML;
pending…
string replaceAll method
return html.replaceAll('&', '&amp;').replaceAll('"', '&quot;').replaceAll("'", '&#39;').replaceAll('<', '&lt;').replaceAll('>', '&gt;');
pending…
replace chain on prototype
return html.htmlEncode();
pending…
single replace with pre-defined function
return html.replace(/[&"'\<\>]/g, replaceEntity);
pending…
.nodeValue
DOMtext.nodeValue = html;
return DOMnative.innerHTML;
pending…
replace new RegExp
return html
  .replace(new RegExp("&", "g"), '&amp;')
  .replace(new RegExp("\"", "g"), '&quot;')
  .replace(new RegExp("'", "g"), '&#39;')
  .replace(new RegExp("<", "g"), '&lt;')
  .replace(new RegExp(">", "g"), '&gt;');
pending…
replace new RegExp literal
return html
  .replace(RE_amp, '&amp;')
  .replace(RE_dquo, '&quot;')
  .replace(RE_squo, '&#39;')
  .replace(RE_lt, '&lt;')
  .replace(RE_gt, '&gt;');
pending…
replace new RegExp literal prototype
return html.htmlSafe();
pending…
split join replace (prototype)
return html.replaceAll2('&', '&amp;').replaceAll2('"', '&quot;').replaceAll2("'", '&#39;').replaceAll2('<', '&lt;').replaceAll2('>', '&gt;');
pending…
split join inline
return html.split('&').join('&amp;').split('"').join('&quot;').split("'").join('&#39;').split('<').join('&lt;').split('>').join('&gt;');
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