String concatenation (v79)

Revision 79 of this benchmark created on


Description

Different ways to concatenate strings together

Test runner

Ready to run.

Testing in
TestOps/sec
Direct concatenation
var foo = "a" + "b" + "c" + "d";
ready
Individual += statements
var foo = "a";
foo += "b";
foo += "c";
foo += "d";
ready
Individual statements
var foo = "a";
foo = foo + "b";
foo = foo + "c";
foo = foo + "d";
ready
Using Array#join
var arr = [];
arr.push("a");
arr.push("b");
arr.push("c");
arr.push("d");

var foo = arr.join('');
ready
Single individual statement
var foo = "a";
foo += "b" + "c" + "d";
ready
Function based (for)
function concat(arr) {
  var len = arr.length;
  for (s = "", i = 0; i < len; i++) {
    s += arr[i]
  }
  return s;
}

var arr = [];
arr.push("a");
arr.push("b");
arr.push("c");
arr.push("d");
var foo = concat(arr);
ready
concat
var foo = "a";
foo = foo.concat("b");
foo = foo.concat("c");
foo = foo.concat("d");
ready
Using Array#join without param
var arr = [];
arr.push("a");
arr.push("b");
arr.push("c");
arr.push("d");

var foo = arr.join();
ready
Aggregate concat
var foo = "a".concat("b", "c", "d");
ready
Direct concatenation (vars)
var a = "a";
var b = "b";
var c = "c";
var d = "d";
var foo = a + b + c + d;
ready
reduce with direct concatenation
var arr = [];
arr.push("a");
arr.push("b");
arr.push("c");
arr.push("d");

var foo = arr.reduce(function(prev, curr, index, array) {
  return prev + curr;
}, "");
ready

Revisions

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