Sub topic 18
Runtime Support and Feature Detection
The iterator helpers in this tutorial are part of ES2025. Every major engine ships them now, but some code still runs on older browsers, older Node.js releases, embedded engines, or behind build tools that assume an older target. This chapter covers where the helpers are available, how to check for them at runtime, and what to do when they are missing.
This chapter assumes you already know how Iterator.prototype relates to generators and built-in iterators (covered earlier in this tutorial). It also assumes you are comfortable with typeof checks and basic module imports.
Where the Helpers Ship
The proposal reached Stage 4 in late 2024 and was included in ES2025. Engines shipped it before that date, so the first supporting version of each runtime is older than the specification itself.
| Runtime | First version with `Iterator.prototype.map` and friends | Notes |
|---|---|---|
| Chrome / Edge (V8) | 122 | Also covers Opera and other Chromium browsers |
| Node.js | 22 | Node 20 and earlier have no `Iterator` global |
| Deno | 1.42 | Inherits support from V8 |
| Firefox (SpiderMonkey) | 131 | |
| Safari (JavaScriptCore) | 18.4 | Older iOS devices that cannot update stay unsupported |
Two consequences follow from this table. First, a project that supports Node 20 or Safari 17 cannot assume the helpers exist. Second, Safari was last to ship, so a "works in Chrome and Firefox" test is not enough on its own.
What Transpilers Do and Do Not Do
Babel, TypeScript, esbuild, and SWC rewrite syntax. Iterator helpers are not syntax. They are methods on a built-in prototype, so a transpiler leaves gen().map(fn) exactly as written. If the target runtime lacks the method, the call fails at runtime with TypeError: gen(...).map is not a function.
This gives you three separate concerns:
- Types. TypeScript only knows about the methods if your
libsetting includes them. Use TypeScript 5.6 or later with"lib": ["ESNext"](or"ES2025"in versions that offer it). Without that, the compiler reports thatmapdoes not exist on the generator type. A type error here does not mean the runtime lacks support. - Runtime. Only the engine version decides whether the method exists.
- Polyfill. A polyfill library such as core-js can install the methods on older engines.
Detecting Support at Runtime
The spine's basic check looks for the global and one method:
const supported =
typeof Iterator !== 'undefined' &&
typeof Iterator.prototype.map === 'function';
console.log(supported);The typeof Iterator !== 'undefined' guard matters. In Node 20, referencing Iterator directly throws a ReferenceError, because the global does not exist. Using typeof on an undeclared name returns 'undefined' and does not throw.
Checking every method you use
A runtime that has map almost certainly has the rest, but polyfills can be partial and some builds are configured to include only certain modules. If your code depends on several helpers, check all of them:
const REQUIRED = ['map', 'filter', 'take', 'drop', 'flatMap', 'toArray', 'reduce'];
function hasIteratorHelpers() {
if (typeof Iterator === 'undefined' || typeof Iterator.from !== 'function') {
return false;
}
return REQUIRED.every(
(name) => typeof Iterator.prototype[name] === 'function'
);
}
console.log(hasIteratorHelpers());Checking the objects you actually iterate
The methods come from the prototype chain, so a check on Iterator.prototype is a check on everything that inherits from it. If you want to test the real objects your code will handle, probe them directly:
function* probe() {}
const generatorOk = typeof probe().map === 'function';
const arrayIterOk = typeof [].values().map === 'function';
const mapIterOk = typeof new Map().keys().map === 'function';
console.log({ generatorOk, arrayIterOk, mapIterOk });All three are true in a supporting runtime and false in an older one. This check also catches an odd environment where someone has replaced or frozen a prototype.
Choosing a Strategy
Once you know whether support exists, there are three common responses.
Option 1: Require a minimum runtime
If you control the environment, for example a server pinned to Node 22 or later, declare that requirement and use the helpers directly. In package.json:
{
"engines": {
"node": ">=22"
}
}npm only warns about an engines mismatch by default, so add a startup check if a wrong version must fail loudly:
if (typeof Iterator === 'undefined' || typeof Iterator.prototype.take !== 'function') {
throw new Error('This program requires iterator helpers (Node.js 22 or later).');
}Option 2: Load a polyfill
core-js implements the full proposal. Import it once, at the very top of your entry module, before any code that builds a pipeline:
import 'core-js/actual/iterator';
function* naturals() {
let n = 0;
while (true) yield n++;
}
console.log(naturals().map((n) => n * 2).take(3).toArray()); // [0, 2, 4]The polyfill installs a global Iterator and its prototype methods only where they are missing. On a runtime with native support it leaves the native ones in place. The cost is bundle size and a global side effect, which library authors usually want to avoid.
Option 3: Write small fallbacks
A library that must not modify globals can wrap the few operations it needs in plain generator functions. Generators give you laziness by themselves:
function* mapIter(iterable, fn) {
let i = 0;
for (const value of iterable) yield fn(value, i++);
}
function* filterIter(iterable, fn) {
let i = 0;
for (const value of iterable) if (fn(value, i++)) yield value;
}
function* takeIter(iterable, limit) {
if (limit <= 0) return;
let i = 0;
for (const value of iterable) {
yield value;
if (++i >= limit) return;
}
}
function* naturals() {
let n = 0;
while (true) yield n++;
}
const result = [...takeIter(mapIter(filterIter(naturals(), (n) => n % 2 === 0), (n) => n * n), 3)];
console.log(result); // [0, 4, 16]Because for...of calls return() on the source when the generator returns early, takeIter closes the underlying iterator just as native take does. These fallbacks do differ from the native helpers in some ways. takeIter does not throw a RangeError for negative or NaN limits, and its argument check only runs on the first next() call instead of when the function is called. Nested calls also read inside-out, which is harder to follow than a chain.
You can also choose between the two paths at runtime:
const take = hasIteratorHelpers()
? (iter, n) => iter.take(n)
: (iter, n) => takeIter(iter, n);Common Mistakes
- Treating a TypeScript error as missing runtime support, or the reverse. Updating
libsilences the compiler but does not add the methods to Node 20. - Detecting once in a test and assuming production matches. The browsers your users run decide support, not your development machine.
- Polyfilling after use. An import that runs after a module has already built a pipeline cannot help that module. Put the polyfill first in the entry point.
- Assuming sync support implies async support. Detect
AsyncIteratorfeatures separately.
Exercise
A developer writes const ok = Iterator.prototype.map !== undefined; to detect support and ships it. On Node 20 the program crashes before reaching any fallback. Why, and how do you fix the check?
Show answer
Node 20 has no Iterator global, so evaluating Iterator.prototype throws a ReferenceError. Guard with typeof, which is safe on undeclared names: const ok = typeof Iterator !== 'undefined' && typeof Iterator.prototype.map === 'function';.
Exercise
Your project compiles TypeScript to ES2020 output with Babel and targets Safari 17. You use items.values().filter(f).toArray(). The build succeeds. What happens at runtime in Safari 17, and what are two ways to make it work?
Show answer
Transpilers do not add built-in methods, so Safari 17 throws a TypeError because filter is not a function on the array iterator. Fix it by importing a polyfill such as core-js/actual/iterator at the entry point, or by replacing the chain with generator-based fallback functions or plain array methods (items.filter(f)).
Exercise
Why is typeof [].values().map === 'function' a reasonable detection check, and what does it confirm that checking Iterator.prototype.map alone does not?
Show answer
Array iterators inherit from Iterator.prototype, so the probe finds map through the same prototype chain your real code uses. It confirms that this particular built-in iterator actually reaches the method, which would catch an environment where the prototype chain or the method has been altered, not just that the method exists on Iterator.prototype.