escaping html

JavaScript performance comparison

Test case created by Benjamin Santalucia

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  var str = 'this <h1 class="a"> is a title </h1>';

};
</script>

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
createTextNode
var div = document.createElement('div');
div.appendChild(document.createTextNode(str));
return div.innerHTML.replace(/"/g, '&quot;');
pending…
createTextNode no doublequote escape
var div = document.createElement('div');
div.appendChild(document.createTextNode(str));
return div.innerHTML;
pending…
regexp
var MAP = {
  '&': '&amp;',
  '<': '&lt;',
  '>': '&gt;',
  '"': '&quot;',
  "'": '&#39;'
};

return str.replace(/[&<>'"]/g, function(c) {
  return MAP[c];
});
pending…

You can edit these tests or add even more tests to this page by appending /edit to the URL.

Compare results of other browsers

0 Comments