String concat vs regex replace

JavaScript performance comparison

Test case created

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 unknown unknown
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. Here’s a list of current revisions for this page:

0 comments

Add a comment