.replace() vs. .split().join()

JavaScript performance comparison

Revision 6 of this test case created by dongy

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  function rndchar() {
    var charset = "abcdefghijklmnopqrstuvwxyz";
    return charset[Math.floor(Math.random() * charset.length)];
  }
  
  function rndstr(n) {
    var re = "";
    for (var i = 0; i < n; ++i) re += rndchar();
    return re;
  }
  var mylongstr = rndstr(10000),
      longfind = rndstr(5),
      longfindreg = new RegExp(longfind, 'g'),
      longrep = rndstr(5),
      single = rndchar(),
      singlereg = new RegExp(single, 'g');

};
</script>

Test runner

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

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
split(single).join('');
mylongstr.split(single).join('');
pending…
replace(singlereg, '');
mylongstr.replace(singlereg, '');
pending…
split(longfind).join('');
mylongstr.split(longfind).join('');
pending…
replace(longfindreg, '');
mylongstr.replace(longfindreg, '');
pending…
split(longfind).join(longrep);
mylongstr.split(longfind).join(longrep);
pending…
replace(longfindreg, longrep);
mylongstr.replace(longfindreg, longrep);
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.

0 Comments