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

Revision 4 of this benchmark created on


Setup

var mystring = 'okay.this.is.a.string';

Test runner

Ready to run.

Testing in
TestOps/sec
.replace(/\./g,' ')
result = mystring.replace(/\./g,' ');
ready
.replace(new RegExp(".","gm")," ")
result = mystring.replace(new RegExp(".","gm")," ");
ready
.split('.').join(' ')
result = mystring.split('.').join(' ');
ready
replaceAll-Fagner-Brack
/**
 * ReplaceAll by Fagner Brack (MIT Licensed)
 * Replaces all occurrences of a substring in a string
 */
String.prototype.replaceAll = function(token, newToken, ignoreCase) {
    var str, i = -1, _token;
    if((str = this.toString()) && typeof token === "string") {
        _token = ignoreCase === true? token.toLowerCase() : undefined;
        while((i = (
            _token !== undefined? 
                str.toLowerCase().indexOf(
                            _token, 
                            i >= 0? i + newToken.length : 0
                ) : str.indexOf(
                            token,
                            i >= 0? i + newToken.length : 0
                )
        )) !== -1 ) {
            str = str.substring(0, i)
                    .concat(newToken)
                    .concat(str.substring(i + token.length));
        }
    }
return str;
};

result = mystring.replaceAll('.', ' ');
ready
while
/*
http://vhspiceros.blogspot.fi/2008/12/replace-all-en-javascript.html
*/
function replaceAll( text, busca, reemplaza ){
	  while (text.toString().indexOf(busca) != -1)
	      text = text.toString().replace(busca,reemplaza);
	  return text;
}
result = mystring.replaceAll('.', ' ');
ready

Revisions

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