.replace() vs. .split().join() vs .replaceAll() (v31)

Revision 31 of this benchmark created by Tamil Vendhan on


Setup

var mystring = 'okay.this.is.a.string';
  
  
  window.replaceAll = function( _str, token, newToken, ignoreCase ) {
      var _token;
      var str = _str;
      var i = -1;
  
      if ( typeof token === "string" ) {
  
          if ( ignoreCase ) {
  
              _token = token.toLowerCase();
  
              while( (
                  i = str.toLowerCase().indexOf(
                      token, i >= 0 ? i + newToken.length : 0
                  ) ) !== -1
              ) {
                  str = str.substring( 0, i ) +
                      newToken +
                      str.substring( i + token.length );
              }
  
          } else {
              return _str.split( token ).join( newToken );
          }
  
      }
  return str;
  }

Test runner

Ready to run.

Testing in
TestOps/sec
replaceAll
result = replaceAll(mystring, '.', ' ');
ready
.replace(new RegExp(".","gm")," ")
result = mystring.replace(new RegExp(".","gm")," ");
ready
.split('.').join(' ')
result = mystring.split('.').join(' ');
ready
replaceAll
result = replaceAll(mystring, '.', ' ');
ready

Revisions

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