concat arrays (v4)

Revision 4 of this benchmark created by V1 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 arr1 = [];
  var arr2 = [];
  var arr3 = [];
  var bigArray = new Array(100);
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
big concat
arr1 = arr1.concat(bigArray);
ready
big Array prototype push
Array.prototype.push.apply(arr2, bigArray)
ready
big Array prototype unshift
Array.prototype.unshift.apply(arr3, bigArray)
ready
small concat
var arr = [];
arr = arr.concat(arr, bigArray);
ready
small Array prototype push
var arr = [];
Array.prototype.push.apply(arr, bigArray);
ready
small Array prototype unshift
var arr = [];
Array.prototype.unshift.apply(arr, bigArray);
ready
Big Array.prototye.concat
var arr1 = Array.prototype.concat.apply(arr1, bigArray);
ready
small Array.prototype.concat
var arr = [];
arr = Array.prototype.concat.apply(arr, bigArray);
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