Sub topic 18
Runtime Support and Feature Detection
This chapter assumes you know the helper methods covered earlier (map, filter, take, drop, toArray) and are comfortable with generator functions. It covers where iterator helpers are available, how to check for them at runtime, and what to do when they are missing.
Where Iterator Helpers Ship
Iterator helpers are part of ES2025. Engines shipped them before the spec was finalized, so the minimum versions are fairly recent:
| Runtime | First version with iterator helpers |
|---|---|
| Chrome / Edge | 122 |
| Firefox | 131 |
| Safari | 18.4 |
| Node.js | 22 |
If your code runs only on servers or tools you control, such as Node 22 or later, you can use the helpers directly. Code that runs in browsers has to account for users on older versions, embedded webviews, and long-lived enterprise installs.
Detecting Support at Runtime
Check for both the global Iterator and one of its helper methods. Older engines don't define Iterator as a global at all, so the typeof check prevents a ReferenceError.
const supported =
typeof Iterator !== 'undefined' &&
typeof Iterator.prototype.map === 'function';
console.log(supported);Engines shipped the helpers as a group, so checking map is normally enough. If your code depends on a specific method such as flatMap or Iterator.from, check that method as well.
Fallback Options
Polyfill
Libraries such as core-js install spec-compliant versions of the helpers on Iterator.prototype. After the polyfill loads, the rest of your code can be written as if every engine supported the helpers natively. This is usually the right choice for application code.
Small generator fallbacks
If you only need one or two operations, standalone generator functions behave the same way and work in any engine that supports generators:
function* mapIter(iter, fn) {
let i = 0;
for (const value of iter) yield fn(value, i++);
}
function* takeIter(iter, limit) {
if (limit <= 0) return;
let i = 0;
for (const value of iter) {
yield value;
if (++i >= limit) return;
}
}
function* naturals() {
let n = 0;
while (true) yield n++;
}
console.log([...takeIter(mapIter(naturals(), (n) => n * 10), 3)]);
// [0, 10, 20]Leaving a for...of loop early calls return() on the source, so the cleanup behaviour matches the native take. These fallbacks are simplified. For example, takeIter does not throw a RangeError for a negative limit.
Exercise
A teammate writes if (Iterator.prototype.map) { ... } as a feature check. Why can this crash in an older browser, and how do you fix it?
Show answer
In engines without iterator helpers, the global Iterator is often not defined at all. Reading Iterator.prototype then throws a ReferenceError before the check can run. Guard with typeof Iterator !== 'undefined' first, then check typeof Iterator.prototype.map === 'function'.
Exercise
Your project compiles cleanly with TypeScript and Babel, but values().map(...) throws "map is not a function" in Safari 17. What happened?
Show answer
Type checking and transpilation do not add missing runtime methods. Safari 17 predates iterator helpers, which arrived in Safari 18.4. Load a polyfill or use a generator-based fallback.