String concatenation (v26)

Revision 26 of this benchmark created on


Description

Different ways to concatenate strings together

Preparation HTML

<script>
  var one = String.fromCharCode(Math.floor(Math.random() * 128));
  var two = String.fromCharCode(Math.floor(Math.random() * 128));
  var three = String.fromCharCode(Math.floor(Math.random() * 128));
  var four = String.fromCharCode(Math.floor(Math.random() * 128));
  var arr = [one, two, three, four]
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
Direct concatenation
var foo = one + two + three + four;
ready
Individual += statements
var foo = one;
foo += two;
foo += three;
foo += four;
ready
Individual statements
var foo = one;
foo = foo + two;
foo = foo + three;
foo = foo + four;
ready
Using Array#join
var foo = arr.join('');
ready
Single individual statement
var foo = one;
foo += two + three + four;
ready
Function based (for)
function concat(arr) {
  len = arr.length;
  for (s = "", i = 0; i < 4; s += arr[i], i++);
  return s;
}

var foo = concat(arr);
ready

Revisions

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