String concatenation

JavaScript performance comparison

Revision 26 of this test case created

Info

Different ways to concatenate strings together

Preparation code

<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

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
Direct concatenation
var foo = one + two + three + four;
pending…
Individual += statements
var foo = one;
foo += two;
foo += three;
foo += four;
pending…
Individual statements
var foo = one;
foo = foo + two;
foo = foo + three;
foo = foo + four;
pending…
Using Array#join
var foo = arr.join('');
pending…
Single individual statement
var foo = one;
foo += two + three + four;
pending…
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);
pending…

Compare results of other browsers

Revisions

You can edit these tests or add even more tests to this page by appending /edit to the URL. Here’s a list of current revisions for this page:

0 comments

Add a comment