string regexp replace vs loop indexof replaces (v12)

Revision 12 of this benchmark created on


Setup

var r = /\./g;
    
    function regexReplace(str){
      str.replace(/\./g, ',');
    }
    
    function stringReplace(str){
      while (str.indexOf('.') !== -1) {
        str = str.replace('.', ',');
      }
    }
    
    function hardLoop(str){
       for(var i=0; i< str.length; i++) if(str[i]===".") str[i]=","
    }

Test runner

Ready to run.

Testing in
TestOps/sec
Regex replace
var str = '1.2.3.4.5.6.7.8.9.0';
str.replace(/\./g, ',');
ready
String replace
var str = '1.2.3.4.5.6.7.8.9.0';
while (str.indexOf('.') !== -1) {
  str = str.replace('.', ',');
}
 
ready
Regex replace (function call)
var str = '1.2.3.4.5.6.7.8.9.0';

regexReplace(str);
ready
String replace (function call)
var str = '1.2.3.4.5.6.7.8.9.0';

stringReplace(str);
ready
hardloop
var str = '1.2.3.4.5.6.7.8.9.0';

hardLoop(str);
ready

Revisions

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