split/join vs replace

JavaScript performance comparison

Revision 2 of this test case created by Ryan


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

Preparation code

  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();

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("\\'");
replace small
window.test_str = createSmall().replace(/'/g, "\\'");
split/join medium
window.test_str = createMedium().split("'").join("\\'");
replace medium
window.test_str = createMedium().replace(/'/g, "\\'");
split/join large
window.test_str = createLarge().split("'").join("\\'");
replace large
window.test_str = createLarge().replace(/'/g, "\\'");
split/join xtralg
window.test_str = createXtralg().split("'").join("\\'");
replace xtralg
window.test_str = createXtralg().replace(/'/g, "\\'");

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

Steven commented :

So from this graph it looks like there isn't a significant advantage to using Split/Join vs. just using replace. I'd wager to say split/join slightly more convenient just because it's easy to read and doesn't require regular expressions (Powerful but complex).

Comment form temporarily disabled.

Add a comment