string regexp replace vs loop indexof replaces (v8)

Revision 8 of this benchmark created on


Setup

var r = /\./g;
    
    function a ( str )
    {
       return str.replace(/\./g, ',');
    }
    
    function b ( str )
    {
       while (str.indexOf('.') !== -1) {
          str = str.replace('.', ',');
       }
       return str
    }
    
    function c ( str )
    {
       return str.replace(r, ',');
    }
    
    // BROKEN: strings are immutale in javascript
    function d ( str )
    {
       for(var i=0; i< str.length; i++)
          if(str[i]===".")
             str[i]=","
       return str
    }
    
    function e ( str )
    {
       var lastLocation;
       while ((lastLocation = str.indexOf('.', lastLocation)) !== -1) {
          str = str.replace('.', ',');
       }
       return str
    }
    
    function f ( str )
    {
       return str.split('.').join(',')
    }

Test runner

Ready to run.

Testing in
TestOps/sec
string replace regexp
a('1.2.3.4.5.6.7.8.9.0')
ready
loop indexof
b('1.2.3.4.5.6.7.8.9.0')
 
ready
precompiled regexp
c('1.2.3.4.5.6.7.8.9.0')
ready
simple loop (BROKEN)
d('1.2.3.4.5.6.7.8.9.0') // test nothing. strings are immutable.
 
ready
enhanced while
e('1.2.3.4.5.6.7.8.9.0')

 
ready
split & join
f('1.2.3.4.5.6.7.8.9.0')
ready

Revisions

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