Checking iterator helper support…
Eager — array methodstouches all 5
[1,2,3,4,5] .map(n => n * 2) .filter(n => n > 5) // want first 2 — but processes ALL
0
operations
0
arrays built
// press Run…
Lazy — iterator helpersstops at 2
[1,2,3,4,5].values() .map(n => n * 2) .filter(n => n > 5) .take(2).toArray()
0
operations
0
arrays built
// press Run…
Eager runs map on all 5, then filter on all 5. Lazy pulls one value through the whole chain and stops at 2.
Impossible with array methods. An infinite generator has no length — Array.prototype.filter would hang forever. Iterator helpers pull lazily, so take(n) stops the generator exactly when it has enough.
Sequence
take(n) 10
pulled 0 values from the generator
The generator is infinite, yet the tab never freezes — because nothing is pulled until toArray() asks, and take(n) stops asking once it has n results.
Most iterators are single-use. Consume one with a terminal method and it is spent — walk it again and you get nothing. This bites when your logic needs two passes over the same data.
const iter = [1,2,3,4,5].values().map(n => n * 2); const first = iter.toArray(); // walk #1 const second = iter.toArray(); // walk #2 — same iterator
First .toArray()
// —
Second .toArray() (same iterator)
// —