Replace All vs Split/Join (v13)

Revision 13 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() {
      var weiredChar = String.fromCharCode(57344);
    String.prototype.replaceAllWithWeiredChar = function(replace_what, replace_with) {
        
        var a = '';
        var b = this;
        do {
          a = b;
          b = a.replace(replace_what, weiredChar);
        } while (a !== b)
        
        do {
          a = b;
          b = a.replace(weiredChar, replace_with);
        } while (a !== b)
          
        return b;
    };
    })();

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

Revisions

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