String concatenation (v28)

Revision 28 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 = String.fromCharCode(Math.floor(Math.random() * 128)) + String.fromCharCode(Math.floor(Math.random() * 128)) + String.fromCharCode(Math.floor(Math.random() * 128)) + String.fromCharCode(Math.floor(Math.random() * 128));
ready
Individual += statements
var foo = String.fromCharCode(Math.floor(Math.random() * 128));
foo += String.fromCharCode(Math.floor(Math.random() * 128));
foo += String.fromCharCode(Math.floor(Math.random() * 128));
foo += String.fromCharCode(Math.floor(Math.random() * 128));
ready
Individual statements
var foo = String.fromCharCode(Math.floor(Math.random() * 128));
foo = foo + String.fromCharCode(Math.floor(Math.random() * 128));
foo = foo + String.fromCharCode(Math.floor(Math.random() * 128));
foo = foo + String.fromCharCode(Math.floor(Math.random() * 128));
ready
Using Array#join
var foo = [String.fromCharCode(Math.floor(Math.random() * 128)),
String.fromCharCode(Math.floor(Math.random() * 128)), String.fromCharCode(Math.floor(Math.random() * 128)), String.fromCharCode(Math.floor(Math.random() * 128))].join('');
ready
Single individual statement
var foo = String.fromCharCode(Math.floor(Math.random() * 128));
foo += String.fromCharCode(Math.floor(Math.random() * 128)) + String.fromCharCode(Math.floor(Math.random() * 128)) + String.fromCharCode(Math.floor(Math.random() * 128));
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.