javascript array join vs string concat

Benchmark created by hyunsook on


Preparation HTML

<script>
  var myArray = ['a', 'b', 'c', 'd'];
</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

Revisions

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