Two weeks ago, the HTTP 203 Podcast released a video called Is reduce() bad?.
It’s an interesting episode.
If you’ve been reading my articles for a while, you know that I’m an avid fan of
Array.reduce() method also has a weird syntax that’s often hard for people to understand. The video isn’t really an attack on
reduce() so much as an attack on developers who try to make their code too clever, something I’ve written about before as well.
Some key takeaways for me:
- Don’t use
Array.reduce()when another array method (
filter(), etc.) would work just as well.
- Do use it when it you can replace a series of chained array methods that might hurt performance with a single one (I think the podcast actually disagrees with me here).
- Do use it when you would otherwise need to use a series of different manipulations. Sometimes
reduce()actually makes things simpler.
It bears repeating one more time: don’t use clever code when simple code will work just as well.