3 ms·
>using map() has historically been about 10x slower than using a for loop. Wow, really? Any idea why? Seems odd when map() imposes fewer guarantees and can be
by dTal 10y ago
>using map() has historically been about 10x slower than using a for loop.
Wow, really? Any idea why? Seems odd when map() imposes fewer guarantees and can be implemented, in the worst case, with a for() loop...
- dahart 10y agoI don't really know why, and I have heard there are large discrepancies from browser to browser. It is a function call per iteration, or even a closure, so I'm guessing it may often be difficult to recognize and optimize out the function call, or maybe some browsers haven't gotten around to it yet, I don't know. This also depends on what you're comparing. Map always allocates a new array. In my Chrome 57 right now, map() is 4x slower than new array + a for loop of push(). But it's 100x faster to re-use an existing array... (a for loop of array element assignment)