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

JavaScript performance comparison

Revision 5 of this test case created by rmaksim

Info

A solution for replacement of strings.

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var mystring1 = 'okay this is a string';
    var mystring2 = 'okay.this.is.a.string';
};
</script>

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,'.')
result1 = mystring1.replace(/ /g,'.');
pending…
.split(' ').join('.')
result1 = mystring1.split(' ').join('.');
pending…
.replace(/\./g,' ')
result2 = mystring2.replace(/\./g,' ');
pending…
.split('.').join(' ')
result2 = mystring2.split('.').join(' ');
pending…

Compare results of other browsers

Revisions

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:

0 comments

Add a comment