javascript array join vs string concat

JavaScript performance comparison

Revision 16 of this test case created by

Preparation code

<script>
  var myArray = ['a', 'b', 'c', 'd','f','e','g','h','i','j','k','l','m'];
</script>
    

Test runner

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

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
array join (use push)
var output = '',
  tmp = [],
  i;
for (i = 0; i < myArray.length; i++) {
  tmp.push(myArray[i]);
}

output = tmp.join('');
pending…
array join (use length)
var output = '',
  tmp = [],
  i;
for (i = 0; i < myArray.length; i++) {
  tmp[tmp.length] = myArray[i];
}

output = tmp.join('');
pending…
string concat
var output = '',
  i;
for (i = 0; i < myArray.length; i++) {
  output += myArray[i];
}
pending…
array join (use length) - optimized
var output = '',
  tmp = [],
  i;
for (i = 0, il = myArray.length; i < il; i++) {
  tmp[tmp.length] = myArray[i];
}

output = tmp.join('');
pending…
string concat - optimized
var output = '',
  i;
for (i = 0, il = myArray.length; i < il; i++) {
  output += myArray[i];
}
pending…
array join (use push) - optimized
var output = '',
  tmp = [],
  i;
for (i = 0, il = myArray.length; i < il; i++) {
  tmp.push(myArray[i]);
}

output = tmp.join('');
pending…
join initialized array
output = myArray.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.

0 Comments