3 ms·
> the "default way" is so inefficient A big reason why I say screw map, filter, reduce, etc. and my "default way" never stopped being ordinary for loops.
by pwdisswordfish0 5y ago
> the "default way" is so inefficient
A big reason why I say screw map, filter, reduce, etc. and my "default way" never stopped being ordinary for loops.
- brundolf 5y agobastawhiz's (correct) point is that it's easy in JS to have the best of both worlds: write your code as multiple, decoupled passes, with the performance of a for loop. The only problem is that in JS you have to go out of your way to do it. But those of us who know about it can, and generally should, do so.
- andyferris 5y agoThough I have found generators in JS often come with an overhead compared to a well-written for loop. (Some languages like rust or Julia will inline all the closures for you and remove all the overhead). Still a great way to avoid intermediate allocations, though.
- satao 5y agocan you point me to a good tutorial showing how to do this? i'm a conflicted web dev regarding loops. I love the simplicity that map/filter have, but I hate that they require looping over the same array more than once, which makes me choose for loops over them... Never thought I could have the best of both worlds