String concat vs regex replace vs Array join (v6)

Revision 6 of this benchmark created on


Description

What's the fastest way to build a string?

Setup

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

Ready to run.

Testing in
TestOps/sec
String join
var rendered = "Hello, my name is ";
rendered = rendered + view.name;
rendered = rendered + " and my value is ";
rendered = rendered + view.value;
ready
Regex.replace
var rendered = render(template, view);
ready
Array.join
var rendered = ["Hello, my name is ", view.name, " and my value is ", view.value].join('');
ready
String.concat
var str = "";
var rendered = str.concat("Hello, my name is ", view.name, " and my value is ", view.value);
ready

Revisions

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