Sub topic 15
Iterator Helper Methods
This chapter assumes you know how generators produce iterators and how the lazy pipeline from the previous chapter works, where map and take were hand-written generator functions. Iterator helpers are built-in methods on Iterator.prototype, added in ES2025. They give you the same lazy pipeline without writing those functions yourself.
The working model
Every built-in iterator inherits from Iterator.prototype. That includes generator objects, array.values(), map.entries(), and string[Symbol.iterator](). Because of that inheritance, they all get the helper methods.
There are two kinds of helpers:
| Kind | Methods | Returns |
|---|---|---|
| Lazy | `map`, `filter`, `take`, `drop`, `flatMap` | A new iterator. No work happens until something pulls a value. |
| Eager | `toArray`, `reduce`, `forEach`, `some`, `every`, `find` | A final result. These methods consume the iterator. |
A lazy helper pulls a value from its source only when the next stage asks for one. That is why chaining helpers on an infinite generator is safe:
function* naturals() {
let n = 0;
while (true) yield n++;
}
const squares = naturals()
.map(n => {
console.log('squaring', n);
return n * n;
})
.take(2); // nothing has been logged yet
console.log(squares.toArray());
// squaring 0
// squaring 1
// [ 0, 1 ]Common errors
Calling helpers on an iterable instead of an iterator
Arrays already have their own map and filter methods. Those array methods are eager and return arrays. Map and Set have no map method at all. To get the lazy versions, call a method that returns an iterator first:
const prices = new Map([['tea', 3], ['cake', 5], ['pie', 4]]);
const cheap = prices.entries()
.filter(([, price]) => price < 5)
.map(([name]) => name)
.toArray();
console.log(cheap); // [ 'tea', 'pie' ]
const firstTwo = [10, 20, 30].values().take(2).toArray();
console.log(firstTwo); // [ 10, 20 ]Hand-built iterators lack the helpers
The iterator objects you wrote by hand in earlier chapters are plain objects. They do not inherit from Iterator.prototype. Iterator.from wraps such an object, or any iterable, in an iterator that has the helpers:
const counter = {
i: 0,
next() {
return this.i < 3
? { value: this.i++, done: false }
: { value: undefined, done: true };
}
};
console.log(Iterator.from(counter).map(x => x * 2).toArray()); // [ 0, 2, 4 ]Reusing a consumed chain
Helpers return iterators, and iterators work only once. After toArray() runs, the chain is exhausted. To iterate again, rebuild the chain from a fresh source.
Exercise
What does this log, and how many times does the filter callback run? function* naturals() { let n = 0; while (true) yield n++; } console.log(naturals().filter(n => n % 3 === 0).drop(1).take(2).toArray());
Show answer
It logs [ 3, 6 ]. The filter callback runs 7 times, for 0 through 6. drop(1) discards 0, take(2) accepts 3 and 6, and after the second value take stops pulling, so no more values are requested.
Exercise
Why does new Set([1, 2]).map(x => x + 1) throw a TypeError, and how do you fix it?
Show answer
Set is an iterable, not an iterator, and Set.prototype has no map method. Call .values() first to get an iterator: new Set([1, 2]).values().map(x => x + 1).toArray() returns [ 2, 3 ].