Iterator helpers: map, filter and take without building arrays
array.filter().map().slice( 0, 10 ) is readable, but on a large list it does all the work for every element and builds two full intermediate arrays, only to keep ten items at the end. ES2025’s iterator helpers add map(), filter(), take(), drop(), flatMap() and friends to iterators. They are lazy: each element passes through the whole chain before the next one is read, and the chain stops as soon as take() has enough.
Problem: An admin screen shows the first 20 products that match a search from a client-side list of 30,000 products. The code filters all 30,000, maps them all to display objects, then slices 20, and every keystroke costs 40 ms.
Solution: Turn the array into an iterator with values(), chain filter(), map() and take( 20 ), and collect the result with toArray(). Use the same helpers on generators, for example for paginated REST API data, and on Map and Set iterators.
Lazy instead of eager
// eager: filters 30,000, maps every match, then keeps 20
const eager = products
.filter( ( p ) => p.name.toLowerCase().includes( term ) )
.map( haToRow )
.slice( 0, 20 );
// lazy: stops after the 20th match
const lazy = products.values()
.filter( ( p ) => p.name.toLowerCase().includes( term ) )
.map( haToRow )
.take( 20 )
.toArray();
products.values() returns an array iterator, and since ES2025 every built-in iterator inherits the helper methods from Iterator.prototype. If the first 20 matches are among the first 500 products, the lazy version reads 500 elements instead of 30,000 and calls haToRow() 20 times instead of for every match.
The helpers
map( fn ),filter( fn ),flatMap( fn ): like the array methods, but lazy.take( n ),drop( n ): the first n elements, or all but the first n.toArray(): collect into an array.reduce(),some(),every(),find(),forEach(): consume the iterator;some()andfind()stop at the first hit.Iterator.from( obj ): wraps any iterable or iterator-like object so it gets the helpers.
An iterator can be consumed only once. Keep the array and create a new iterator with values() for each use.
With generators and Map
function* haRange( start, end ) {
for ( let i = start; i < end; i++ ) yield i;
}
const firstOddSquares = haRange( 0, Infinity )
.filter( ( n ) => n % 2 )
.map( ( n ) => n * n )
.take( 5 )
.toArray(); // [ 1, 9, 25, 49, 81 ]
const stockByProduct = new Map( [ [ 101, 0 ], [ 102, 7 ], [ 103, 0 ] ] );
const outOfStock = stockByProduct.entries()
.filter( ( [ , qty ] ) => qty === 0 )
.map( ( [ id ] ) => id )
.toArray(); // [ 101, 103 ]
An infinite generator is fine, because take() ends the chain. Map.prototype.entries() used to require Array.from() before any filtering; now the helpers work directly on it.
Async data
The helpers are synchronous. For paginated REST requests, collect with an async generator and Array.fromAsync(), as in ES2024 in WordPress scripts; async iterator helpers are a separate proposal that is not part of the language yet.
NOTE: Iterator helpers are in Chrome and Edge 122 (February 2024), Firefox 131 (October 2024) and Safari 18.4 (March 2025), and in Node.js 22. For admin screens used by people on older Safari versions, check typeof Iterator !== 'undefined' && Iterator.prototype.take or include the core-js polyfill. For small arrays, the eager array methods are just as fast and simpler to debug; the lazy chain pays off for large data and early exits.