split/join vs RegExp vs replaceAll

JavaScript performance comparison

Revision 47 of this test case created by

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  var str1 = "Test test test abc test ... ";
  var strTest = [
    str1,
    str1 + str1 + str1 + str1 + str1,
    'Test',
    ''
  ];
  
  function replaceAll(str, search_str, rep_str) {
    if (~str.indexOf(search_str)) {
      var prev;
      do {
        prev = str;
      } while ((str = str.replace(search_str, rep_str)) !== prev);
    }
    return str;
  }
  

};
</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 & join
for (var i = 0; i < strTest.length; ++i)
  strTest[i].split('abc').join('');
pending…
replace w/RegExp
for (var i = 0; i < strTest.length; ++i)
  strTest[i].replace(/abc/g, '');
pending…
replaceAll while
for (var i = 0; i < strTest.length; ++i)
  replaceAll(strTest[i], 'abc', '');
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