DEV Community

Discussion on: 2 Ways to Merge Arrays in JavaScript

Collapse
 
agronick profile image
Kyle Agronick • Edited

Surprisingly concat is way faster. Doing any type of spread is 91% slower. jsperf.com/concat-vs-spreader/1

Edit: Actually, it seems to depend on the size of the array. With 10 elements in each array spread is faster. With 100 elements in each array spread is about 75% slower.

Collapse
 
samanthaming profile image
Samantha Ming

Interesting, thanks for writing out the test! Good to keep in mind if you need to optimize the code. But I wonder as spread become more popular, if the browser will start optimizing it ... maybe concat is faster because it’s been around longer and the browser is deciphering it 🤔

Collapse
 
agronick profile image
Kyle Agronick

I wondered that too. You would think it would just call concat internally. I'm not sure what would cause the huge difference.