Learn JavaScript

Lesson 4 of 9 · Arrays and the Array Methods: The map, filter, reduce Pipeline

Module 7 · Arrays and the Array Methods: The map, filter, reduce Pipeline

When to Use the Pipeline, When to Use a Loop, and How to Choose

Free with an accountReading

Kenji has just learned map, filter and reduce, and now he writes everything as a pipeline. His code review comes back with two comments. One says "this reduce is very hard to follow, please use a loop". The other, on a different file, says "this loop is four lines of bookkeeping, please use filter". Kenji is confused. Which one is right?

Checking what this lesson needs for you…

What is inside When to Use the Pipeline, When to Use a Loop, and How to Choose

  1. Readability first
  2. A loop against the pipeline: a measured run
  3. map against forEach
  4. reduce against a loop
  5. When the loop wins: stopping early and carrying state
  6. The comparison table