JavaScript performance comparison

Revision 18 of this test case created

Preparation code

<script src="">
Benchmark.prototype.setup = function() {
    var html = document.body.innerHTML;
    var map = {
      "&": "&amp;",
      "'": "&#39;",
      '"': "&quot;",
      "<": "&lt;",
      ">": "&gt;"
    var tempElement = $(document.createElement("div"));

Test runner

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

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
multiple replace()
return html.replace(/&/g, '&amp;').replace(/"/g, '&quot;').replace(/'/g, '&#39;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
single replace with map
return html.replace(/[&"'\<\>]/g, function(c) {
  return map[c];
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;";

use jQuery
return $('<div/>').text(html).html();
use jQuery with pre-created element
return tempElement.text(html).html();

Compare results of other browsers


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:

1 comment

Pete commented :

These results compared with the original set in April 2012 show a total reversal in the outcome.

Has jQuery really been optimized that much in a year?

Comment form temporarily disabled.

Add a comment