String concat vs regex replace vs Array join (v11)

Revision 11 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]
        })
        };
    
    Array.prototype.render = function(){
        var l = this.length
                , c = 0
                , s = '';
    
        while(c < l){
                s+= this[c++];
        }
    
        return s;
    }
    
    var join = function(){
     var s = '',
         c = 0,
         l = arguments.length
    
      while(c<l)
        s += arguments[c++];
    
     return s;
    }

Test runner

Ready to run.

Testing in
TestOps/sec
String join
var rendered = "Hello, my name is " + view.name + " and my value is " + view.value;
ready
Regex.replace
var rendered = template.replace(/\{name\}/g,view.name).replace(/\{value\}/g,view.value);
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
Reference Array concat
var rendered = ["Hello, my name is ", view.name, " and my value is ", view.value];
var l = rendered.length;
var c = 0;
var s = '';

while (c < l) {
    s += rendered[c++];
}
ready
Array Shift Concat
var rendered = ["Hello, my name is ", view.name, " and my value is ", view.value];
var l = rendered.length;
var c = 0;
var s = '';

while((c++) < l) {
    s += rendered.shift();
}
ready
Array.prototype
["Hello, my name is ", view.name, " and my value is ", view.value].render();
ready
String args
join("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.