String concatenation

JavaScript performance comparison

Revision 23 of this test case created

Info

Different ways to concatenate strings together

Preparation code

<script>
  var arr = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
</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' + 'e' + 'f' + 'g' + 'h' + 'i' + 'j' + 'k' + 'l' + 'm' + 'n' + 'o' + 'p' + 'q' + 'r' + 's' + 't' + 'u' + 'v' + 'w' + 'x' + 'y' + 'z';
pending…
Individual += statements
var foo = 'a';
foo += 'b';
foo += 'c';
foo += 'd';
foo += 'e';
foo += 'f';
foo += 'g';
foo += 'h';
foo += 'i';
foo += 'j';
foo += 'k';
foo += 'l';
foo += 'm';
foo += 'n';
foo += 'o';
foo += 'p';
foo += 'q';
foo += 'r';
foo += 's';
foo += 't';
foo += 'u';
foo += 'v';
foo += 'w';
foo += 'x';
foo += 'y';
foo += 'z';
pending…
Individual statements
var foo = 'a';
foo = foo + 'b';
foo = foo + 'c';
foo = foo + 'd';
foo = foo + 'e';
foo = foo + 'f';
foo = foo + 'g';
foo = foo + 'h';
foo = foo + 'i';
foo = foo + 'j';
foo = foo + 'k';
foo = foo + 'l';
foo = foo + 'm';
foo = foo + 'n';
foo = foo + 'o';
foo = foo + 'p';
foo = foo + 'q';
foo = foo + 'r';
foo = foo + 's';
foo = foo + 't';
foo = foo + 'u';
foo = foo + 'v';
foo = foo + 'w';
foo = foo + 'x';
foo = foo + 'y';
foo = foo + 'z';
pending…
Using Array#join
var foo = arr.join('');
pending…
Single individual statement
var foo = 'a';
foo += 'b' + 'c' + 'd' + 'e' + 'f' + 'g' + 'h' + 'i' + 'j' + 'k' + 'l' + 'm' + 'n' + 'o' + 'p' + 'q' + 'r' + 's' + 't' + 'u' + 'v' + 'w' + 'x' + 'y' + 'z';
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…

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