itermoth
  • { l.name }
itermoth 0.1.0ES2025 iterator helpers, plus Iterator.concat from ES2026

Iterator helpers today, exactly as the spec defines them.

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.

npm i itermoth
ESM and CommonJS, zero dependencies. Copied to the clipboard
toArray(take(filter(map(naturals(), x => x * x), x => x % 2), 5))
pulled0in the jar0return()

Tap a flower to change its callback. The garden replays the new chain, run by itermoth in your browser.

Playground

Pull only what you need

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.

Source
Chain
  1. { s.op }
    • { o.label }
(no steps yet)
Finish with
Pull trace
{ lane.label }
{ c.text }
{ $store.pg.pulls }values pulled from the source { $store.pg.closes }return() calls on the source

This chain never ends on an infinite source, so the playground stops it after 60 pulls. Add a take() step to finish it.

itermoth (spec code)
{ $store.pg.result }
native helpers
{ $store.pg.nativeResult }
This browser has no native iterator helpers to compare with. itermoth is running the spec algorithms itself.
Same values, same pulls, same return() callsThe traces differ
The same chain in code
How to

How to use iterator helpers on Node 18 and older browsers

Iterator.prototype.map and its siblings are missing before Node 22 and in older browsers. Three steps give you the same behavior everywhere.

  1. Install

    Add the package. It has no dependencies and ships ESM, CommonJS and type declarations.

    npm i itermoth
  2. Import and chain

    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]
  3. Or add the methods

    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]
Why

Why itermoth exists

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.

Features

What you get

Everything the spec asks for, nothing it does not, in a package small enough to forget about.

Lazy to the last value

Nothing is pulled until you ask. take(5) on an infinite generator pulls five values and stops.

Closes what it opens

return() is called on early exit, when a callback throws and when an argument is invalid. Never when the source is exhausted.

Native first

Each native helper is probed once. If it follows ES2025 it is used directly; older behavior falls back to the spec code.

Spec exact

Written from the ES2025 text, step by step. Argument checks, ToNumber on limits, the order of property reads, re-entrancy errors.

  • Iterator.prototype.map27.1.4.8
  • Iterator.prototype.take27.1.4.11
  • Iterator.prototype.flatMap27.1.4.7
  • Iterator.from27.1.3.2.1

Tiny

About 2.6 kB minified and gzipped for everything, tree shakeable, sideEffects false, zero dependencies.

Tested against the engine

4,400 seeded random scenarios replay every property read and call against the native helpers and must match exactly.

Any iterable

Arrays, Sets, Maps, strings, generators, plain objects with a next method. No wrapping, no prototype changes.

ESM, CommonJS and types

Hand written declarations with generics and type guards. Works in Node 18+, Bun, Deno and browsers.

Opt-in install()

Adds only what is missing to Iterator and Iterator.prototype, with the right names, lengths and attributes.

Examples

Code examples

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]
API

API

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).

FunctionReturnsNotes
map(source, mapper)IteratorApplies mapper to each value.
filter(source, predicate)IteratorKeeps values where predicate is truthy.
take(source, limit)IteratorAt most limit values, then closes the source. RangeError for NaN or negative limits.
drop(source, limit)IteratorSkips limit values. RangeError for NaN or negative limits.
flatMap(source, mapper)IteratorFlattens one level of iterables or iterators. String results are a TypeError.
reduce(source, reducer, initial?)anyFolds values. Without an initial value the first value is used; an empty source is a TypeError.
toArray(source)ArrayCollects every value.
forEach(source, fn)undefinedCalls fn for each value.
some(source, predicate)booleanStops and closes at the first truthy result.
every(source, predicate)booleanStops and closes at the first falsy result.
find(source, predicate)value | undefinedThe first matching value, then closes.
from(value)IteratorIterator.from: returns iterators that already inherit from Iterator.prototype, wraps the rest.
concat(...iterables)IteratorIterator.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.

Benchmarks

Benchmarks

Operations per second over 10,000 numbers, median of seven runs after warmup. Bars are relative to the native helpers. Higher is better.

Operations per second relative to native iterator helpers map + filter + take + reduceplain loop: 34.9x nativenative1.00x 3,391/sitermoth0.99x 3,342/sitermoth spec code0.61x 2,083/score-js-pure 3.51.00.42x 1,426/ses-iterator-helpers 1.4.00.04x 123/smap + toArrayplain loop: 10.5x nativenative1.00x 3,992/sitermoth1.08x 4,318/sitermoth spec code0.77x 3,087/score-js-pure 3.51.00.46x 1,840/ses-iterator-helpers 1.4.00.06x 223/sflatMap + toArrayplain loop: 8.0x nativenative1.00x 1,048/sitermoth0.95x 991/sitermoth spec code0.95x 999/score-js-pure 3.51.00.68x 715/ses-iterator-helpers 1.4.00.04x 38/sfind near the endplain loop: 21.6x nativenative1.00x 7,910/sitermoth1.05x 8,333/sitermoth spec code1.28x 10,141/score-js-pure 3.51.00.46x 3,629/ses-iterator-helpers 1.4.00.15x 1,194/s

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

Size

Size

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.

RuntimeHelpers usedStatus
Node 18, 20spec codetested in CI
Node 22, 24nativetested in CI and locally
Bun 1.3nativetested in CI and locally
Deno 2nativetested in CI
Browsersnative 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.

Minified and gzipped size itermoth2.6 kBcore-js-pure 3.51.018.4 kBes-iterator-helpers 1.4.022.6 kB
Compare

How it compares

Two well known packages also implement these helpers. All numbers come from installing each one and measuring it on the same machine.

Featureitermoth 0.1.0core-js-pure 3.51.0es-iterator-helpers 1.4.0
Standalone function per helper
Uses the engine's helpers when they conform
Iterator.concat
Direct runtime dependencies0016
All helpers, min + gzip2.6 kB18.4 kB22.6 kB
Speed against native (map, filter, take, reduce)0.99x0.42x0.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.

FAQ

Questions

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.

Stop computing values nobody asked for.

Add itermoth, chain helpers on anything iterable, and let the engine do the work wherever it already can.

npm i itermoth