Lazy to the last value
Nothing is pulled until you ask. take(5) on an infinite generator pulls five values and stops.
map, filter, take, drop, flatMap, reduce and the rest, as standalone functions for any iterable or iterator. Your engine's native helpers when it has conforming ones, the spec algorithm step by step when it does not.
toArray(take(filter(map(naturals(), x => x * x), x => x % 2), 5))
Tap a flower to change its callback. The garden replays the new chain, run by itermoth in your browser.
Build a chain on an infinite generator. A cell lights up only when a value is really pulled through that step; the faded columns were never computed. The same chain runs on itermoth's spec code and on this browser's native helpers, and the two traces are compared.
This chain never ends on an infinite source, so the playground stops it after 60 pulls. Add a take() step to finish it.
Iterator.prototype.map and its siblings are missing before Node 22 and in older browsers. Three steps give you the same behavior everywhere.
Add the package. It has no dependencies and ships ESM, CommonJS and type declarations.
npm i itermoth
Pass any iterable or iterator as the first argument. Nothing runs until you consume the result.
import { map, take, toArray } from 'itermoth'; toArray(take(map(new Set([1, 2, 3]), (x) => x * 10), 2)); // [10, 20]
Applications that prefer method chains can call install() once at startup. It only adds what the engine lacks.
import { install } from 'itermoth'; install(); new Set([1, 2, 3]).values().map((x) => x * 10).toArray(); // [10, 20, 30]
The iterator helpers landed in ES2025 and are already native in current Chrome, Edge, Firefox, Safari, Node 22 and later, Deno and Bun. Libraries still have to run on Node 18 and 20 and on older browsers, and they usually should not patch globals to get there.
itermoth gives you the same eleven helpers as plain functions that take the source first. When the engine ships conforming helpers they are called directly, so you pay a function call and nothing more. When it does not, the algorithms from the spec run instead, including the details that are easy to get wrong: when next is read, which errors close the source, and when return() must not be called.
It also includes Iterator.from and Iterator.concat, which reached stage 4 for ES2026, and an opt-in install() for applications that want the methods on Iterator.prototype.
Everything the spec asks for, nothing it does not, in a package small enough to forget about.
Nothing is pulled until you ask. take(5) on an infinite generator pulls five values and stops.
return() is called on early exit, when a callback throws and when an argument is invalid. Never when the source is exhausted.
Each native helper is probed once. If it follows ES2025 it is used directly; older behavior falls back to the spec code.
Written from the ES2025 text, step by step. Argument checks, ToNumber on limits, the order of property reads, re-entrancy errors.
About 2.6 kB minified and gzipped for everything, tree shakeable, sideEffects false, zero dependencies.
4,400 seeded random scenarios replay every property read and call against the native helpers and must match exactly.
Arrays, Sets, Maps, strings, generators, plain objects with a next method. No wrapping, no prototype changes.
Hand written declarations with generics and type guards. Works in Node 18+, Bun, Deno and browsers.
Adds only what is missing to Iterator and Iterator.prototype, with the right names, lengths and attributes.
Plain functions that take the source first. Copy any of these into a file and run it on Node 18 or later.
import { map, filter, take, toArray } from 'itermoth';
function* naturals() {
for (let n = 0; ; n++) yield n;
}
// Lazy: only the values that are needed are ever computed.
const firstOddSquares = toArray(
take(filter(map(naturals(), (x) => x * x), (x) => x % 2 === 1), 5),
);
// [1, 9, 25, 49, 81]
Every function turns source into an iterator first: it calls source[Symbol.iterator]() when that exists and otherwise uses source itself. Strings are iterated by code point. Callbacks receive (value, index).
| Function | Returns | Notes |
|---|---|---|
| map(source, mapper) | Iterator | Applies mapper to each value. |
| filter(source, predicate) | Iterator | Keeps values where predicate is truthy. |
| take(source, limit) | Iterator | At most limit values, then closes the source. RangeError for NaN or negative limits. |
| drop(source, limit) | Iterator | Skips limit values. RangeError for NaN or negative limits. |
| flatMap(source, mapper) | Iterator | Flattens one level of iterables or iterators. String results are a TypeError. |
| reduce(source, reducer, initial?) | any | Folds values. Without an initial value the first value is used; an empty source is a TypeError. |
| toArray(source) | Array | Collects every value. |
| forEach(source, fn) | undefined | Calls fn for each value. |
| some(source, predicate) | boolean | Stops and closes at the first truthy result. |
| every(source, predicate) | boolean | Stops and closes at the first falsy result. |
| find(source, predicate) | value | undefined | The first matching value, then closes. |
| from(value) | Iterator | Iterator.from: returns iterators that already inherit from Iterator.prototype, wraps the rest. |
| concat(...iterables) | Iterator | Iterator.concat (ES2026): every argument is checked at once and opened only when reached. |
| install() | string[] | Adds only the missing globals and methods. Returns the names it added. |
Non-callable callbacks throw TypeError and invalid limits throw RangeError, in both cases after closing the source, as ES2025 requires.
Operations per second over 10,000 numbers, median of seven runs after warmup. Bars are relative to the native helpers. Higher is better.
A hand written for loop is still 8 to 35 times faster than any iterator helper, native ones included. Use helpers for clarity and laziness, loops for hot paths. itermoth spec code is what runs on engines without native helpers (Node 18 and 20, older browsers).
Machine: AMD Ryzen 7 7800X3D 8-Core Processor, win32 x64, Node v24.16.0
All thirteen functions plus install(), minified with terser and gzipped. The alternatives were measured the same way over every module their matching entry points load.
| Runtime | Helpers used | Status |
|---|---|---|
| Node 18, 20 | spec code | tested in CI |
| Node 22, 24 | native | tested in CI and locally |
| Bun 1.3 | native | tested in CI and locally |
| Deno 2 | native | tested in CI |
| Browsers | native in current versions, spec code in older ones | - |
Iterator.concat is native only where the engine ships it; everywhere else itermoth runs the spec code.
Two well known packages also implement these helpers. All numbers come from installing each one and measuring it on the same machine.
| Feature | itermoth 0.1.0 | core-js-pure 3.51.0 | es-iterator-helpers 1.4.0 |
|---|---|---|---|
| Standalone function per helper | |||
| Uses the engine's helpers when they conform | |||
| Iterator.concat | |||
| Direct runtime dependencies | 0 | 0 | 16 |
| All helpers, min + gzip | 2.6 kB | 18.4 kB | 22.6 kB |
| Speed against native (map, filter, take, reduce) | 0.99x | 0.42x | 0.04x |
Both alternatives are solid and cover far more than iterators. itermoth is for when you want only these helpers, exactly as specified, at the smallest cost.
A library should not change globals that the application owns. Functions are tree shakeable and work the same on every runtime. Applications that do want the methods can call install() once.
Add itermoth, chain helpers on anything iterable, and let the engine do the work wherever it already can.