javascript array join vs string concat (v16)

Revision 16 of this benchmark created on


Preparation HTML

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

Test runner

Ready to run.

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

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

output = tmp.join('');
ready
string concat
var output = '',
  i;
for (i = 0; i < myArray.length; i++) {
  output += myArray[i];
}
ready
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('');
ready
string concat - optimized
var output = '',
  i;
for (i = 0, il = myArray.length; i < il; i++) {
  output += myArray[i];
}
ready
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('');
ready
join initialized array
output = myArray.join('');
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.