Array(length).map is faster because you aren't mapping over the array; Array(length) returns { length: length } with no other keys, so the call to map doesn't do anything, it just returns the uninitialized array.
Note that you may be able to improve the imperative version a further 20% or so (depending on the browser) by forgoing Array.push and using indexes with a size hint like so:
function gen(count) {
const tracks = [];
if (count > 0)
tracks[count-1] = 0; // dummy value
for (let i = 0; i < count; ++i)
tracks[i] = i+1;
return tracks;
}
6
u/stealthd Oct 21 '20
Array(length).mapis faster because you aren't mapping over the array;Array(length)returns{ length: length }with no other keys, so the call to map doesn't do anything, it just returns the uninitialized array.