escapeHTML

Benchmark created by rkatic on


Preparation HTML

<script>
  var escapeHTML = function(s) {
   return s.replace(/[\&"<>\\]/g, function(s) {
    switch (s) {
    case "&":
     return "&amp;";
    case "\\":
     return "\\\\";
    case '"':
     return '\"';
    case "<":
     return "&lt;";
    case ">":
     return "&gt;";
    default:
     return s;
    }
   });
  }
  
  var escapeHTML2 = function(s) {
   return s.replace(/[\&"<>\\]/g, repl);
  }
  var repl = function(s) {
   switch (s) {
   case "&":
    return "&amp;";
   case "\\":
    return "\\\\";
   case '"':
    return '\"';
   case "<":
    return "&lt;";
   case ">":
    return "&gt;";
   default:
    return s;
   }
  };
  
  var html = "<span>hohoho!!!</span>";
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
escapeHTML
escapeHTML(html);
ready
escapeHTML2
escapeHTML2(html);
ready

Revisions

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