Global string replacement

Benchmark created by Mathias Bynens on

Description 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
.split('foo').join('bar') replace
'the man and the plan'.split('the').join('a'); // 'a man and a plan'
.replace(/foo/g, 'bar')
'the man and the plan'.replace(/the/g, 'a'); // 'a man and a plan'


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