Concatenation vs Array Join

JavaScript performance comparison

Test case created

Preparation code

  var str, str1, i, j;

Test runner

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

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
for (i = 0; i < 10000; i++) {
 str += 'a';
var tmp = [];
for (j = 0; j < 10000; j++) {
str2 = tmp.join('');

Compare results of other browsers


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:

1 comment

Samuel Reed commented :

This test is incredibly flawed and should not be used. See Rev 16 which seems to get it right.

The issue is that the concatenation test appends to a global var, 'str'. This var grows as the tests are run over and over, leading to a test that slows down massively over time. The array.join test, however, initializes a new array every time it is run.

Notice how the results are almost the opposite of the correct results in Rev 16. String concatenation has been heavily optimized in all modern browsers to do internal array joining and is actually faster than Array.join(). See this StackOverflow comment for more detail.

Comment form temporarily disabled.

Add a comment