- you're adding a few elements to a large array
→ push(...b) is very fast
- you're adding many elements to a large array
→ concat is slightly faster than a loop
- you're adding many elements to a small array
→ concat is much faster than a loop
- you're usually adding only a few elements to any size array
→ loops are about as fast as the limited methods for small additions, but will never throw an exception even if it is not the most performant when you add many elements
- you're writing a wrapper function to always get the maximum performance
→ you'll need to check the lengths of the inputs dynamically and choose the right method, perhaps calling push(...b_part) (with slices of the big b) in a loop.
ms | Asize | Bsize | code
----+-------+-------+------------------------------
~0 | any | any | a.push(...b)
~0 | any | any | a.push.apply(a, b)
480 | 10M | 50 | a = a.concat(b)
0 | 10M | 50 | for (let i in b) a.push(b[i])
506 | 10M | 500k | a = a.concat(b)
882 | 10M | 500k | for (let i in b) a.push(b[i])
11 | 10 | 500k | a = a.concat(b)
851 | 10 | 500k | for (let i in b) a.push(b[i])
참고: https://stackoverflow.com/questions/1374126/how-to-extend-an-existing-javascript-array-with-another-array-without-creating/17368101#17368101