split/join vs replace

JavaScript performance comparison

Revision 2 of this test case created by Ryan

Info

Forcibly de-tuned the benchmark to make Chrome spit out a marginally representative result.

Preparation code

<script>
  var medium = new Array(100).join(" ' "),
      large = new Array(1000).join(" ' "),
      xtralg = new Array(10000).join(" ' ");
 
  function createSmall() {
   return " ' ' ' " + Math.random();
  }
 
  function createMedium() {
   return medium + Math.random();
  }
 
  function createLarge() {
   return large + Math.random();
  }
 
  function createXtralg() {
   return xtralg + Math.random();
  }
</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
split/join small
window.test_str = createSmall().split("'").join("\\'");
pending…
replace small
window.test_str = createSmall().replace(/'/g, "\\'");
pending…
split/join medium
window.test_str = createMedium().split("'").join("\\'");
pending…
replace medium
window.test_str = createMedium().replace(/'/g, "\\'");
pending…
split/join large
window.test_str = createLarge().split("'").join("\\'");
pending…
replace large
window.test_str = createLarge().replace(/'/g, "\\'");
pending…
split/join xtralg
window.test_str = createXtralg().split("'").join("\\'");
pending…
replace xtralg
window.test_str = createXtralg().replace(/'/g, "\\'");
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