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

Revision 52 of this benchmark created on


Setup

var mystring = 'okay.this.is.a.string';
  var reg = new RegExp(".","gm");
  
  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(reg," ");
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.