4 ms·
Using forEach/map/etc. can often result in worst performance, because some JavaScript JITs (SpiderMonkey in particular has trouble with this) aren't able to mon
by vore 5y ago
Using forEach/map/etc. can often result in worst performance, because some JavaScript JITs (SpiderMonkey in particular has trouble with this) aren't able to monomorphize calls to them effectively and end up falling back to the interpreter due to the megamorphic nature of these higher-order functions.
Even for engines that do, you do end up paying a cost for the JIT to warm up to monomorphize these functions.
- yakshaving_jgt 5y agoOk, but how much? And how does the cost compare against the impact of correctness errors? To just say “there is cost” is not so useful; there is cost to everything.
- koshergweilo 5y agoI used to do js gamedev and really it's only a problem in hot loops. Absolutely fine to use it a lot when doing basic webdev/server work
- vore 5y agoAs always, benchmark and profile! However, from first principles, this is one of the degenerate cases of certain JITs that often catches people off guard, but has a straightforward transformation to avoid the pessimizing behavior – especially if your loops end up looking like they're particularly hot.