Replace All vs Split/Join (v12)

Revision 12 of this benchmark created on


Setup

str = "Test abc test test abc test abc test test abc test abc test test abc test abc test test abc test abc test test abc test abc test test abc test abc test test abc test abc test test abc test abc test test abc test abc test test abc test abc test test abc test abc test test abc test abc test test abc test abc test test abc test abc test test abc test abc test test abc test abc test test abc test abc test test abc test abc test test abc test abc test test abc test abc test test abc test abc test test abc test abc test test abc test abc test test abc test abc test test abc test abc test test abc test abc test test abc test abc test test abc test abc test test abc test abc test test abc test abc test test abc test abc test test abc test abc test test abc test abc test test abc test abc test test abc test abc test test abc test abc test test abc test abc test test abc test abc test test abc test abc test test abc test abc test test abc test abc test test abc test abc test test abc test abc test test abc test abc test test abc test abc test test abc test abc test test abc test abc test test abc test abc test test abc test abc test test abc test abc test test abc test abc test test abc test abc test test abc test abc test test abc test abc test test abc test abc test test abc test abc test test abc test abc test test abc test abc test test abc test abc test test abc test abc test test abc test abc test test abc test abc test test abc test abc test test abc test abc test test abc test abc test test abc test abc test test abc test abc test test abc test abc test test abc test abc test test abc test abc test test abc test abc test test abc test abc test test abc test abc test test abc test abc test test abc test abc test test abc test...";
    
    reg_exp = new RegExp('abc', 'g');
    
    replaceAllRecursive = function(string, replace_what, replace_with, prevstring) {
      if (prevstring && string === prevstring)
        return string;
      prevstring = string.replace(replace_what, replace_with);
      return replaceAllRecursive(prevstring, replace_what, replace_with, string);
    };
    
    replaceAllLoopy = function(string, replace_what, replace_with) {
      while(true){
        var newstring = string.replace(replace_what, replace_with);
        if (newstring === string) return string;
        string = newstring;
        }
    };
    
    String.prototype.replaceAll = function(replace_what, replace_with) {
                var oldstring = this;
                while (true) {
                        var newstring = oldstring.replace(replace_what, replace_with);
                        if (newstring === oldstring) return oldstring;
                        oldstring = newstring;
                        }
                };

Test runner

Ready to run.

Testing in
TestOps/sec
split & join
str = str.split("abc").join("");
ready
replace with inline regexp
str = str.replace(/abc/g, '');
ready
replace with cached regexp
str = str.replace(reg_exp, '');
ready
recursive replace()
str = replaceAllRecursive(str, "abc", "");
ready
iterative replace()
str = replaceAllLoopy(str, "abc", "");
ready
iterative replace() as String method
str = str.replaceAll("abc", "");
ready

Revisions

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