concat arrays (v6)

Revision 6 of this benchmark created by Leonardo Dutra on


Description

There are different ways of adding concat arrays. You can use array.concat or leverage the Array.prototype methods.

So, what will be faster? Array.concat because, it's build for it.. Or a prototype function?

It seems that when you need concat large arrays, the Array.prototype.push() will save you tons of time. For all other operations the array.concat would be the best option.

Preparation HTML

<script>
  var bigArray = [],
      l = 20,
      i = 0,
      n = 0,
      array;
  
  while (++i < l) { // forces value, no undefined; optimized
   bigArray[i] = 1;
  }
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
[].concat
//(array = []) used to avoid memory leaks, found on tests 

(array = []).concat(bigArray);
ready
Array.prototype.concat.call
Array.prototype.concat.call((array = []), bigArray);
ready
Array.prototype.concat.apply
Array.prototype.concat.apply((array = []), bigArray);
ready
[].push
(array = []).push(bigArray);
ready
Array.prototype.push.call
Array.prototype.push.call((array = []), bigArray);
ready
Array.prototype.push.apply
Array.prototype.push.apply((array = []), bigArray);
ready
[].unshift
(array = []).unshift(bigArray);
ready
Array.prototype.unshift.call
Array.prototype.unshift.call((array = []), bigArray);
ready
Array.prototype.unshift.apply
Array.prototype.unshift.apply((array = []), bigArray);
ready
Optimum loop
l = bigArray.length;
n = (array = []).length;
for (i = -1; ++i < l;) {
 array[n++] = bigArray[i];
}
ready
Optimum loop*
n = (i = bigArray.length) + (array = []).length - 1 >> 0;
while (i--) {
 array[n--] = bigArray[i];
}
ready

Revisions

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

  • Revision 1: published by V1 on
  • Revision 4: published by V1 on
  • Revision 5: published by Leonardo Dutra on
  • Revision 6: published by Leonardo Dutra on
  • Revision 7: published by Denis G. on