String concat vs regex replace

JavaScript performance comparison

Test case created

Preparation code

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]})};

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 " + + " and my value is " + view.value;
Using a regex
var rendered = render(template, view);

Compare results of other browsers


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:


Comment form temporarily disabled.

Add a comment