Need the first 3 of a million? map and filter on JavaScript iterators

JavaScript iterator helpers like map, filter and take work on the iterator directly and lazily: they compute the next value only when you ask for it. So they stop once you reach the first results you need, and they even work on a sequence with no end.

Published 2 min read#Web development#Developer tools

You have a list of a million products, and you want the first 3 in stock under a certain price. You write map, then filter, then slice, and the code works. But before you get your first 3, it has gone through all million twice. This article shows how to stop the work as soon as you have what you need.

What are iterator helpers?

Iterator helpers are methods like map, filter and take that work on the iterator itself instead of an array, and return a new iterator. An iterator is something that gives you one value each time you ask for the next one with next().

According to the Iterator page on MDN, the methods are map, filter, take, drop, flatMap, reduce, toArray, forEach, some, every and find. And Iterator.from creates an Iterator from anything you can iterate, like an array.

Why do array methods do extra work?

Array methods do extra work because each one runs over every element and builds a new array before the next one starts. map computes all million, then filter goes over all million, then slice takes 3.

const first3 = products
  .map(withPrice)
  .filter((p) => p.inStock && p.price < 50)
  .slice(0, 3);

That is two intermediate arrays of about a million items, and most of that work you never see.

How does an iterator stop the work early?

Iterator helpers are lazy: they compute the next value only when something asks for it. So take(3) asks for 3 values, each value goes through map and filter on its own, and once there are 3, the work stops.

const first3 = Iterator.from(products)
  .map(withPrice)
  .filter((p) => p.inStock && p.price < 50)
  .take(3)
  .toArray();

The map() page on MDN says it plainly: the main advantage over array methods is that they are lazy, which avoids unnecessary computation. The Iterator page adds that they may save memory, because they go over the iterator once without memorizing intermediate values.

Do they work on a sequence with no end?

Yes, iterator helpers work on a sequence with no end, because they never try to read everything. You can never turn a sequence like that into an array, but you can take what you need from it.

function* ids() {
  let n = 1;
  while (true) yield `order-${n++}`;
}

const next5 = ids().drop(100).take(5).toArray();

The same goes for API pages you fetch one at a time, or the lines of a big file: you stop as soon as you find what you are looking for.

Where do you start now?

The lesson for you as a developer: when you need a small part of something big, ask for it piece by piece instead of computing everything and cutting.

  1. Search your code for map and filter chains that end in slice, find or [0].

  2. Put Iterator.from(...) at the start, and replace slice(0, n) with take(n).toArray().

  3. Leave small arrays where you need every element as they are.

Other methods that save you array copies are in the toSorted article and the Set methods article. All our web development articles are in web development articles.

Where in your code do you walk a whole big array just to take its first item?

FAQ

Do they work in every browser?

In current browsers, yes. The MDN page says they work across the latest devices and browser versions since March 2025, but they might not work in older devices or browsers.

When should I stay with array methods?

When the array is small and you need all of it, or you want to read the result more than once. An iterator is read once; an array stays with you.

How do I turn the result into an array at the end?

With toArray. It is one of the iterator methods, and it creates a new array from the elements the iterator yields.

Share

More articles