Concatenation vs Array Join

JavaScript performance comparison

Revision 18 of this test case created by Xotic750

Preparation code

<script>
  var i,
      str = '',
      tmp = [],
      tmp2 = [],
      cnt = 10000,
      push = Array.prototype.push,
      join = Array.prototype.join;

for (i = 0; i < cnt; i++) {
 push.call(tmp2, 'a');
}
</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 1
for (i = 0, str = ''; i < cnt; i++) {
 str += 'a';
}
pending…
Concatenation 2
for (i = 0, str = ''; i < cnt; i++) {
 str += tmp2[i];
}
pending…
Array.prototype.join 1
for (i = 0, tmp.length = 0; i < cnt; i++) {
 push.call(tmp, 'a');
}

str = join.call(tmp, '');
pending…
Array.prototype.join 2
for (i = 0, tmp.length = 0; i < cnt; i++) {
 tmp[i] = 'a';
}

str = join.call(tmp, '');
pending…
Array.prototype.join 3
str = join.call(tmp2, '');
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