Replace All vs Split/Join (v15)

Revision 15 of this benchmark created on


Setup

str = "Test abc test test abc test...";
    reg_exp = new RegExp('abc', 'g');
    replaceAll = function(string, omit, place, prevstring) {
      if (prevstring && string === prevstring)
        return string;
      prevstring = string.replace(omit, place);
      return replaceAll(prevstring, omit, place, string)
    }

Test runner

Ready to run.

Testing in
TestOps/sec
split & join
str.split("abc").join("");
ready
replace
str.replace(/abc/g, '');
ready
replaceAll custom function
str = replaceAll(str, "abc", "")
ready
Dan's
str.split(' ').map(function(word) {
  return word.charAt(0).toUpperCase() + word.substring(1);
}).join(' ');
ready
matt's
str.replace(/\b('?\S)/g, function(s) {
  return s.toUpperCase()
})
ready

Revisions

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