String concat vs regex replace vs Array join

JavaScript performance comparison

Revision 2 of this test case created by Dave Kinsella

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  var view = {
    name: "test",
    value: 5
  },
      template = "Hello, my name is {name} and my value is {value}",
      render = function fmt(s, a, r) {
      r = /\{([^}]+)\}/g;
      return s.replace(r, function(_, m) {
        return a[m]
      })
      };

};
</script>

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
String join
var rendered = "Hello, my name is " + view.name + " and my value is " + view.value;
pending…
Array.join
var rendered = ["Hello, my name is ", view.name, " and my value is ", view.value].join('');
pending…
Regex.replace
var rendered = render(template, view);
pending…
String.concat
var str = "";
var rendered = str.concat("Hello, my name is ", view.name, " and my value is ", view.value);
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.

0 Comments