String concat vs regex replace

JavaScript performance comparison

Test case created by

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
//This would be an example of a compiled template
var rendered = "Hello, my name is " + view.name + " and my value is " + view.value;
pending…
Using a regex
var rendered = render(template, view);
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