String concatenation

JavaScript performance comparison

Revision 16 of this test case created

Info

Different ways to concatenate strings together

Preparation code

<script>
  var arr = ['a', 'b', 'c', 'd']
</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 = 'a' + 'b' + 'c' + 'd';
pending…
Individual += statements
var foo = 'a';
foo += 'b';
foo += 'c';
foo += 'd';
pending…
Individual statements
var foo = 'a';
foo = foo + 'b';
foo = foo + 'c';
foo = foo + 'd';
pending…
Using Array#join
var foo = arr.join('');
pending…
Single individual statement
var foo = 'a';
foo += 'b' + 'c' + 'd';
pending…
Function based (for)
function concat(arr) {
  len = arr.length;
  for (s = "", i = 0; i < len; s += arr[i], i++);
  return s;
}

var foo = concat(arr);
pending…
no concatenation
var foo = "abcd"
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