Replace All vs Split/Join

JavaScript performance comparison

Revision 49 of this test case created by

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  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;
  }

};
</script>

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
split & join
str = str.split("abc").join("");
pending…
replace with inline regexp
str = str.replace(/abc/g, '');
pending…
replace with cached regexp
str = str.replace(reg_exp, '');
pending…
recursive replace()
str = replaceAllRecursive(str, "abc", "");
pending…
iterative replace()
str = replaceAllLoopy(str, "abc", "");
pending…
iterative replace() as String method
str = str.replaceAll("abc", "");
pending…
replaceAllLoop2
str = replaceAllLoop2(str, "abc", "");
pending…

Compare results of other browsers

Revisions

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

0 Comments