html escaping via dom

JavaScript performance comparison

Test case created by kmichel

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    function coerceToString(val) {
      return String((val === null || val === undefined) ? '' : val);
    }
      var rAmp = /&/g,
          rLt = /</g,
          rGt = />/g,
          rApos = /\'/g,
          rQuot = /\"/g,
          hChars = /[&<>\"\']/;
   
    var dom_escape = (function() {
      var el = document.createElement('b');
      if (el.textContent) {return (function(str) {
        return hChars.test(str) ? (el.textContent = coerceToString(str), el.innerHTML) : str;
      })} else {return (function(str) {
        return hChars.test(str) ? (el.innerText = coerceToString(str), el.innerHTML) : str;
      })}
    })();
   
    var hogan_escape = (function() {
   
   
      return (function(str) {
        str = coerceToString(str);
        return hChars.test(str) ? str.replace(rAmp, '&amp;').replace(rLt, '&lt;').replace(rGt, '&gt;').replace(rApos, '&#39;').replace(rQuot, '&quot;') : str;
      });
    })();
   
   
    var split_escape = function(str) {
       var input = coerceToString(str).split(/([&<>\"\'])/g);
       var inputLengthM1 = input.length-1;
       var output = [];
       for (var i = 0; i < inputLengthM1; i += 2) {
          var c = input[i+1];
          output.push(input[i]);
          output.push(
             c == "'" ? '&#39;'  :
             c == '"' ? '&quot;' :
             c == '&' ? '&amp;' :
             c == '<' ? '&lt;' : '&gt;');
       }
       output.push(input[inputLengthM1]);
       return output.join('');
    }
   
    var split_concat_escape = function(str) {
       var input = coerceToString(str).split(/([&<>\"\'])/g);
       var inputLengthM1 = input.length-1;
       var output = input[inputLengthM1];
       for (var i = 0; i < inputLengthM1; i += 2) {
          var c = input[i+1];
          output = input[i] +
          (
             c == "'" ? '&#39;'  :
             c == '"' ? '&quot;' :
             c == '&' ? '&amp;' :
             c == '<' ? '&lt;' : '&gt;') + output;
       }
       return output;
    }
   
    var roseta = {
     "'" : '&#39;',
     '"' : '&quot;',
     '&' : '&amp;',
     '<' : '&lt;' ,
     '>' : '&gt;'};
    var split_concat_hash_escape = function(str) {
       var input = coerceToString(str).split(/([&<>\"\'])/g);
       var inputLengthM1 = input.length-1;
       var output = input[inputLengthM1];
       for (var i = 0; i < inputLengthM1; i += 2)
          output = input[i] + roseta[input[i+1]] + output;
       return output;
    }
};
</script>

Test runner

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

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
dom
dom_escape("It's a kind of magic");
dom_escape("Can you feel the love tonight");
dom_escape("<EVIL> & <EVAL>");
dom_escape("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec libero purus, consequat ac vulputate vitae, sodales ut elit. Proin vel cursus libero. Vivamus facilisis sagittis sem, quis dictum diam suscipit a. In hac habitasse platea dictumst.<> Curabitur dapibus velit at felis dapibus ullamcorper. Vestibulum in orci suscipit tellus vestibulum interdum. Sed eu nisl id eros sollicitudin tempor at a felis. Morbi ac consequat felis. Morbi dapibus gravida erat, vitae porta dolor pharetra non. Suspendisse ac urna ut odio consequat tristique. In volutpat massa eget erat scelerisque pharetra tempus nisi auctor. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Donec vel massa dolor. Nulla facilisi.");
pending…
hogan
hogan_escape("It's a kind of magic");
hogan_escape("Can you feel the love tonight");
hogan_escape("<EVIL> & <EVAL>");
hogan_escape("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec libero purus, consequat ac vulputate vitae, sodales ut elit. Proin vel cursus libero. Vivamus facilisis sagittis sem, quis dictum diam suscipit a. In hac habitasse platea dictumst.<> Curabitur dapibus velit at felis dapibus ullamcorper. Vestibulum in orci suscipit tellus vestibulum interdum. Sed eu nisl id eros sollicitudin tempor at a felis. Morbi ac consequat felis. Morbi dapibus gravida erat, vitae porta dolor pharetra non. Suspendisse ac urna ut odio consequat tristique. In volutpat massa eget erat scelerisque pharetra tempus nisi auctor. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Donec vel massa dolor. Nulla facilisi.");
pending…
split join
split_escape("It's a kind of magic");
split_escape("Can you feel the love tonight");
split_escape("<EVIL> & <EVAL>");
split_escape("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec libero purus, consequat ac vulputate vitae, sodales ut elit. Proin vel cursus libero. Vivamus facilisis sagittis sem, quis dictum diam suscipit a. In hac habitasse platea dictumst.<> Curabitur dapibus velit at felis dapibus ullamcorper. Vestibulum in orci suscipit tellus vestibulum interdum. Sed eu nisl id eros sollicitudin tempor at a felis. Morbi ac consequat felis. Morbi dapibus gravida erat, vitae porta dolor pharetra non. Suspendisse ac urna ut odio consequat tristique. In volutpat massa eget erat scelerisque pharetra tempus nisi auctor. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Donec vel massa dolor. Nulla facilisi.");
pending…
split concat
split_concat_escape("It's a kind of magic");
split_concat_escape("Can you feel the love tonight");
split_concat_escape("<EVIL> & <EVAL>");
split_concat_escape("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec libero purus, consequat ac vulputate vitae, sodales ut elit. Proin vel cursus libero. Vivamus facilisis sagittis sem, quis dictum diam suscipit a. In hac habitasse platea dictumst.<> Curabitur dapibus velit at felis dapibus ullamcorper. Vestibulum in orci suscipit tellus vestibulum interdum. Sed eu nisl id eros sollicitudin tempor at a felis. Morbi ac consequat felis. Morbi dapibus gravida erat, vitae porta dolor pharetra non. Suspendisse ac urna ut odio consequat tristique. In volutpat massa eget erat scelerisque pharetra tempus nisi auctor. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Donec vel massa dolor. Nulla facilisi.");
pending…
split concat hash
split_concat_hash_escape("It's a kind of magic");
split_concat_hash_escape("Can you feel the love tonight");
split_concat_hash_escape("<EVIL> & <EVAL>");
split_concat_hash_escape("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec libero purus, consequat ac vulputate vitae, sodales ut elit. Proin vel cursus libero. Vivamus facilisis sagittis sem, quis dictum diam suscipit a. In hac habitasse platea dictumst.<> Curabitur dapibus velit at felis dapibus ullamcorper. Vestibulum in orci suscipit tellus vestibulum interdum. Sed eu nisl id eros sollicitudin tempor at a felis. Morbi ac consequat felis. Morbi dapibus gravida erat, vitae porta dolor pharetra non. Suspendisse ac urna ut odio consequat tristique. In volutpat massa eget erat scelerisque pharetra tempus nisi auctor. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Donec vel massa dolor. Nulla facilisi.");
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:

0 comments

Add a comment