DEV Community

Discussion on: Javascript Array.push is 945x faster than Array.concat 🤯🤔

Collapse
 
milahu profile image
milahu

"what if we just .push elements individually?"

it depends!
in some cases this is even faster than array1.push(...array2)

plus it is not limited by max call stack size = ~100K in chrome, 500K in firefox

Array.prototype._pushArray = function (other) {
  for (var i = 0; i < other.length; i++) this.push(other[i]);
  return this;
};
array1._pushArray(array2)._pushArray(array3)._pushArray(array4);
Enter fullscreen mode Exit fullscreen mode

should be the fastest, simplest and most robust solution