|
|
|
|
|
by asdfasgasdgasdg
2591 days ago
|
|
I thought that the main cause of slowness was the fact that the accumulator array is being copied one time per array to concatenate. That means that the first array is actually being reallocated n times, where n is the number of input arrays. This is not a necessary feature of immutability; it's a problem with this particular use case. Another big problem is that the article's benchmark is busted [1]. The author thought they were just concatenating two arrays of a fixed length a bunch of times. But what's actually happening is that arr1 is being built up because it is reused for each test case. That means that the concat version is doing A LOT of copying of the data. If you fix the test so that each run concatenates only two 50k arrays, concat is faster [2]. [1]: https://jsperf.com/javascript-array-concat-vs-push/226 [2]: https://jsperf.com/javascript-array-concat-vs-push/228 |
|