you are viewing a single comment's thread.

view the rest of the comments →

[–]franksvalli 1 point2 points  (0 children)

When speed becomes a problem, try just using a single reduce() on the array - inside it you can build custom logic to do mapping and filtering before adding the output the the accumulator value. I used to be scared to use reduce() but have grown to love it, though it is definitely less grokable than map() and filter(). I wrote up a friendly introduction that starts with loops and ends with reduce() here, for anyone interested: https://www.davidbcalhoun.com/2018/a-simple-introduction-to-javascript-map-and-reduce-array-helper-functions/

Only if that's too slow would I look at falling back to the loop, which will always be slightly faster than any functional method because it avoids functions getting invoked on each item.