concat arrays (v7)

Revision 7 of this benchmark created by Denis G. 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?

Not sure we could have a final conclusion, since it really depend on your browser. You may also be interested in concat-arguments test cases.

Prototype function are cached in local scope, to avoid any influence of referencing prototyped function. This is a good coding practice in general. Deleted test cases where all incorrect additions of single element containing the array which could not be compared with array concatenation.

Preparation HTML

<script>
  var bigArray = [],
      l = 20,
      i = 0,
      n = 0,
      array, concat = Array.prototype.concat,
      push = Array.prototype.push,
      unshift = Array.prototype.unshift;
  
  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
concat.call((array = []), bigArray);
ready
Array.prototype.concat.apply
concat.apply((array = []), bigArray);
ready
deleted
throw null;
ready
deleted
throw null;
ready
Array.prototype.push.apply
push.apply((array = []), bigArray);
ready
deleted
throw null;
ready
deleted
throw null;
ready
Array.prototype.unshift.apply
unshift.apply((array = []), bigArray);
ready
Optimum increasing loop
l = bigArray.length;
n = (array = []).length;
for (i = -1; ++i < l;) {
 array[n++] = bigArray[i];
}
ready
Optimum decreasing 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