Replace : Regexp VS split + join

JavaScript performance comparison

Test case created by jpvincent

Info

compare the power of .replace against a combination of .split + .join

inspired by http://javascriptweblog.wordpress.com/2010/11/08/javascripts-dream-team-in-praise-of-split-and-join/

Preparation code

<script>
  var sMyString = "the man and the plan";
 
  var oRegexp = /the/g;
 
  var replaceAll = function(sSubject, sFind, sReplaceWith) {
   return sSubject.split(sFind).join(sReplaceWith);
  }
</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
Use regular expression
var sResult = sMyString.replace(oRegexp, 'a');
pending…
Use split + join
var sResult = replaceAll(sMyString, 'the', 'a');
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