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

JavaScript performance comparison

Test case created by Roberto Frega


A solution for replacement of strings.

Preparation code

Benchmark.prototype.setup = function() {
    var mystring = 'okay.this.is.a.string';

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
.replace(/\./g,' ')
result = mystring.replace(/\./g,' ');
.replace(new RegExp(".","gm")," ")
result = mystring.replace(new RegExp(".","gm")," ");
.split('.').join(' ')
result = mystring.split('.').join(' ');

Compare results of other browsers


You can edit these tests or add even more tests to this page by appending /edit to the URL. Here’s a list of current revisions for this page:

1 comment

Ivaylo Slavov commented :

Great to see the different browsers performance comparison. Surprised to see how clumsier are becoming in time one of the most popular ones

Comment form temporarily disabled.

Add a comment