Global string replacement

Benchmark created by Mathias Bynens on


Description

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!

Test runner

Ready to run.

Testing in
TestOps/sec
.split('foo').join('bar') replace
'the man and the plan'.split('the').join('a'); // 'a man and a plan'
ready
.replace(/foo/g, 'bar')
'the man and the plan'.replace(/the/g, 'a'); // 'a man and a plan'
ready

Revisions

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

  • Revision 1: published by Mathias Bynens on
  • Revision 3: published on