html encode

JavaScript performance comparison

Revision 3 of this test case created by WebReflection

Info

Speed test escaping html characters with if statements and regular expressions.

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    function encodeWithIfs(str) {
      var result = ""
      for (var cc = 0; cc < str.length; cc++) {
        var c = str.charAt(cc)
        if (c == "&") result += '&amp;'
        else if (c == '"') result += '&quot;'
        else if (c == "'") result += '&#39;'
        else if (c == "<") result += '&lt;'
        else if (c == ">") result += '&gt;'
        else result += c
      }
      return result
    }
   
    function endcodeWithRegEx(str) {
      return str.replace(/&/g, '&amp;').replace(/"/g, '&quot;').replace(/'/g, '&#39;').replace(/</g, '&lt;').replace(/>/g, '&gt;')
    }
   
    function endcodeWithRegEx2(str) {
      return str.replace(/[&<>"'`]/g, function (chr) {
        return '&#' + chr.charCodeAt(0) + ';'
      })
    }
   
    function encodeWithSwitchAndArray(str) {
      for (var
        result = str.split(""),
        i = 0, length = result.length;
        i < length; i++
      ) {
        switch(result[i]) {
          case "&": result[i] = "&amp;"; break;
          case '"': result[i] = "&quot;"; break;
          case "'": result[i] = "&#39;"; break;
          case "<": result[i] = "&lt;"; break;
          case ">": result[i] = "&gt;"; break;
        }
      }
      return result.join("");
    }
   
    function encodeWithSwitch(str) {
      for (var
        c,
        result = "",
        i = 0, length = str.length;
        i < length; i++
      ) {
        switch(c = str.charCodeAt(i)) {
          case "&": c = "&amp;"; break;
          case '"': c = "&quot;"; break;
          case "'": c = "&#39;"; break;
          case "<": c = "&lt;"; break;
          case ">": c = "&gt;"; break;
        }
        result += c;
      }
      return result;
    }
   
    var encodeWithRegExpAndObject = function (re, o) {
      function place(m) {
        return o[m];
      }
      return function (str) {
        return str.replace(re, place);
      };
    }(
      /[&"'<>]/g,
      {"&":"&amp;", '"':"&quot;", "'":"&#39;", "<":"&lt;", ">":"&gt;"}
    );
   
    function doEncode(func) {
      var a = func("<div></div>")
      var b = func("This is a string with no replacements")
      var c = func("All replacement chars & \" ' < >")
    }
};
</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
ifs
doEncode(encodeWithIfs)
pending…
regex1
doEncode(endcodeWithRegEx)
pending…
regex2
doEncode(endcodeWithRegEx2)
pending…
switch + Array
doEncode(encodeWithSwitchAndArray)
pending…
switch
doEncode(encodeWithSwitch)
pending…
RegExp + Object
doEncode(encodeWithRegExpAndObject);
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