RegexVsSplitReplaceAll

Benchmark created by sam adams on


Description

.replace only replaces first instance of search string. to replace all we can either use a greedy regex or split (split on search and join on replacement). Which is faster tho?

Setup

var str = 'some code with "replacements"';
    var replacement = '\\"';
    var search = '"';
    var regex = new RegExp(search, 'g');

Test runner

Ready to run.

Testing in
TestOps/sec
split-replace
str.split(search).join(replacement);
ready
regex
str.replace(regex, replacement);
ready

Revisions

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

  • Revision 1: published by sam adams on
  • Revision 2: published by Xav Laumonier on