Multiple Replace vs Replace callback

JavaScript performance comparison

Test case created by Kirill Dmitrenko

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var str = [
      Math.random(),
      '%x',
      Math.random(),
      '%y',
      Math.random()
    ].join();
    var r = {x: Math.random(), y: Math.random()};
};
</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
Multi-replace
var str2 = str
  .replace('%x', r.x)
  .replace('%y', r.y);
pending…
Callback
var str2 = str.replace(/%(\w)/g, function(m, k) {
  return r[k];
});
pending…

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

Compare results of other browsers

0 comments

Add a comment