Concatenation vs Array Join

JavaScript performance comparison

Test case created

Preparation code

<script>
  var str, str1, i, j;
</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
Concatenation
for (i = 0; i < 10000; i++) {
 str += 'a';
}
pending…
Array.prototype.join
var tmp = [];
for (j = 0; j < 10000; j++) {
 tmp.push('a');
}
str2 = tmp.join('');
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:

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.

http://stackoverflow.com/a/7299040/832202

Add a comment