Replace All vs Split/Join (v49)

Revision 49 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;
                        }
                };
    
    function replaceAllLoop2(str, string1, string2) {
        while (str != (str = str.replace(string1, string2))){}
        return str;
    }

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
replaceAllLoop2
str = replaceAllLoop2(str, "abc", "");
ready

Revisions

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