Global string replacement

JavaScript performance comparison

Revision 3 of this test case created

Info

http://javascriptweblog.wordpress.com/2010/11/08/javascripts-dream-team-in-praise-of-split-and-join/ praises the use of .split() and .join() for just about anything in JavaScript.

This test case compares their split/join-based replaceAll method with a simple .replace() using the g modifier.

The article had this to say about replaceAll:

It performs almost as well as the native function (the trade off is two extra function calls against a regex match).

Let’s put it to the test!

Preparation code

<script>
  var str = 'the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan the man and the plan ';
</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
.split('foo').join('bar') replace
str.split('the').join('a'); // 'a man and a plan'
pending…
.replace(/foo/g, 'bar')
str.replace(/the/g, 'a'); // 'a man and a plan'
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