Sub topic 11
Array Methods or Iterator Helpers?
This chapter assumes you know Array map and filter, generator functions, and the lazy pipelines from earlier chapters. Both APIs share method names, but they run in different orders and have different costs. This chapter shows how they differ and how to pick one.
Two Evaluation Orders
Array methods are eager. Each step runs over the whole array and returns a new array before the next step starts. Iterator helpers are lazy. Each value goes through the whole chain before the next value is read.
const arrayLog = [];
[1, 2, 3]
.map((x) => { arrayLog.push(`map ${x}`); return x * 2; })
.filter((x) => { arrayLog.push(`filter ${x}`); return x > 2; });
console.log(arrayLog.join(', '));
// map 1, map 2, map 3, filter 2, filter 4, filter 6
const iterLog = [];
[1, 2, 3]
.values()
.map((x) => { iterLog.push(`map ${x}`); return x * 2; })
.filter((x) => { iterLog.push(`filter ${x}`); return x > 2; })
.toArray();
console.log(iterLog.join(', '));
// map 1, filter 2, map 2, filter 4, map 3, filter 6The array version builds a temporary array after map. The iterator version never does. It holds only the value being processed at that moment.
Comparing the Trade-offs
| Concern | Array methods | Iterator helpers |
|---|---|---|
| Intermediate storage | New array at each step | None |
| Infinite or streaming sources | Not possible | Supported |
| Stopping early | Every step still runs on every element | `take`, `find`, and `some` stop pulling |
| Reuse | Results can be read many times | Single pass only |
| Random access, `length`, `sort` | Available | Not available |
| Small in-memory data | Usually fastest, well optimized | Small per-value overhead |
Iterator helpers are not always faster. On a 50-element array that you read several times, array methods are simpler and often quicker. Laziness pays off when the data is large, unbounded, costly to produce, or when you only need part of it.
Mixing Both
You can switch between the two styles. Use helpers to cut a large or infinite source down to size, then call toArray() and use array-only methods on the small result.
function* readings() {
let t = 0;
while (true) yield { t, value: Math.sin(t++) * 100 };
}
const topFive = readings()
.filter((r) => r.value > 50)
.take(20)
.toArray() // now an array
.sort((a, b) => b.value - a.value) // array-only method
.slice(0, 5);
console.log(topFive.length); // 5A Decision Guide
Exercise
You have a 2 million-element array of log lines and need the first 10 lines containing "ERROR", uppercased. Which approach avoids extra work, and why?
Show answer
Use iterator helpers: lines.values().filter((l) => l.includes('ERROR')).map((l) => l.toUpperCase()).take(10).toArray(). The chain stops pulling after the 10th match and builds no intermediate arrays. The array version would filter all 2 million lines first and allocate a new array before uppercasing anything.
Exercise
A component shows a list of 30 products, sorted by price, and renders it twice. Should it use iterator helpers?
Show answer
No. The data is small and lives in memory. It needs sort, and it is read more than once. A consumed iterator would be empty on the second read, so array methods fit better.