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

Revision 6 of this benchmark created by dongy on


Setup

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

Test runner

Ready to run.

Testing in
TestOps/sec
split(single).join('');
mylongstr.split(single).join('');
ready
replace(singlereg, '');
mylongstr.replace(singlereg, '');
ready
split(longfind).join('');
mylongstr.split(longfind).join('');
ready
replace(longfindreg, '');
mylongstr.replace(longfindreg, '');
ready
split(longfind).join(longrep);
mylongstr.split(longfind).join(longrep);
ready
replace(longfindreg, longrep);
mylongstr.replace(longfindreg, longrep);
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.