3 ms·
It’s unrealistic to expect this level of optimization in a generalist whiteboard session. For a specialist, maybe. I’m not specialized in this area. For an arr
by drewrv 4y ago
It’s unrealistic to expect this level of optimization in a generalist whiteboard session. For a specialist, maybe. I’m not specialized in this area.
For an array of 5-10 items the performance hit of foreach is worth it for readability. Maybe an interesting discussion could occur about when to sacrifice readability for performance but you won’t get that by grilling people about minutiae.
- leeoniya 4y ago> For an array of 5-10 items the performance hit of foreach is worth it for readability 100% agree. my point is more about OP's example being used to prove that dumb questions get asked. if it's being asked by a good interviewer, it's usually relevant.
- zepolen 4y ago> For an array of 5-10 items the performance hit of foreach is worth it for readability As always it depends, maybe that component is being called 500 times on the page making it actually 5,000 invocations. I'd FULLY expect a candidate to understand the difference and would ask in a whiteboard session. There is a lot of bloated slow javascript libraries out there and they almost always stem from the fact that most devs don't realize how much overhead function calls introduce. For the interview, there is no discussion necessary, the correct answer is simply: "I used a foreach for readability, because performance doesn't matter in this case because X." or "I used a for because performance matters in this case because Y" End of story.
- aulin 4y agoone could argue that it's a job for the compiler to make sure two equivalent language constructs (map and for loop) perform similarly...
- barrkel 4y agoForeach with functions being passed being slower is an implementation detail. You don't see it in Rust, where the block body is inlined like a generic, and it's perfectly feasible for a dynamic runtime to specialize the pattern and inline based on it being a common practice, never mind polymorphic inline caches which in principle permit inlining in this case.