Replace All vs Split/Join (v4)

Revision 4 of this benchmark created by Tom on


Setup

var str = "Test abc test test abc test...";
    var toReplace = "abc";
    var replaceWith = "derp";
    
    function splitReplace(src, orig, repl) {
      var split = src.split(orig);
      if (split.length === 1) return src;
      return split.join(repl);
    }

Test runner

Ready to run.

Testing in
TestOps/sec
split & join
str.split(toReplace).join(replaceWith);
ready
replace
str.replace(toReplace, replaceWith);
ready
replace with RegExp
str.replace(new RegExp(toReplace, 'g'), replaceWith);
ready
split/join replace in a function
splitReplace(str, toReplace, replaceWith);
ready

Revisions

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