Sub topic 11
Browser Support, Feature Detection, and Fallbacks
This chapter assumes you know the native Set methods from the earlier chapters. You should also be comfortable with spread syntax, Array.prototype.filter, and typeof checks. It covers where the methods are available, how to check for them at runtime, and how to write fallbacks that behave like the native versions.
Where the Methods Exist
The seven methods (union, intersection, difference, symmetricDifference, isSubsetOf, isSupersetOf, isDisjointFrom) are part of ES2025. All major engines shipped them in 2023 and 2024.
| Runtime | First version with all seven methods |
|---|---|
| Safari | 17 |
| Chrome / Edge | 122 |
| Firefox | 127 |
| Node.js | 22 |
| Deno | 1.42 |
Since mid-2024 the methods count as Baseline, meaning they work in current versions of every major browser. Users on older browsers, embedded WebViews, and Node 18 or 20 servers still lack them. If your code runs in any of those places, you need to check for support before calling the methods.
Feature Detection
Feature detection means testing whether a capability exists at runtime instead of guessing from a version number. Each method lives on Set.prototype, so you check its type there:
const hasSetMethods =
typeof Set.prototype.union === "function" &&
typeof Set.prototype.intersection === "function" &&
typeof Set.prototype.difference === "function";
console.log(hasSetMethods); // true in Node 22+, false in Node 20Do not detect support by parsing navigator.userAgent or process.version. Version strings can be wrong or spoofed, and they tell you nothing about polyfills that are already loaded.
Writing Fallbacks
Each operation fits in one or two lines with spread and filter. Wrap them in helper functions that use the native method when it exists:
function union(a, b) {
return typeof a.union === "function" ? a.union(b) : new Set([...a, ...b]);
}
function intersection(a, b) {
return typeof a.intersection === "function"
? a.intersection(b)
: new Set([...a].filter((x) => b.has(x)));
}
function difference(a, b) {
return typeof a.difference === "function"
? a.difference(b)
: new Set([...a].filter((x) => !b.has(x)));
}
function symmetricDifference(a, b) {
if (typeof a.symmetricDifference === "function") {
return a.symmetricDifference(b);
}
const result = new Set(a);
for (const x of b) {
if (result.has(x)) result.delete(x);
else result.add(x);
}
return result;
}
function isSubsetOf(a, b) {
return typeof a.isSubsetOf === "function"
? a.isSubsetOf(b)
: [...a].every((x) => b.has(x));
}
const a = new Set([1, 2, 3]);
const b = new Set([3, 4]);
console.log([...union(a, b)]); // [1, 2, 3, 4]
console.log([...intersection(a, b)]); // [3]
console.log([...difference(a, b)]); // [1, 2]
console.log([...symmetricDifference(a, b)]); // [1, 2, 4]
console.log(isSubsetOf(new Set([3]), b)); // trueEvery fallback builds a new Set and leaves its inputs alone, the same as the native methods. They also compare elements the same way, because has and the Set constructor both use SameValueZero.
Where Fallbacks Differ from Native Behavior
A hand-written fallback is close to the native method but does not match it exactly. These are the differences most likely to cause bugs:
- Argument validation. Native methods throw a
TypeErrorwhen the argument is not set-like. Theunionfallback accepts an array without complaint, because spread works on any iterable. Code that works on an old runtime can then throw after a browser upgrade. - Ordering in
intersection. The native method may walk the smaller set, so result order can follow the argument's order. The fallback always followsa's order. Don't write code that depends on intersection order. - Performance. The native methods check sizes and iterate the smaller side where they can. The fallbacks always walk all of
a.
To make fallbacks stricter, check the argument yourself:
function assertSetLike(obj) {
if (
typeof obj?.size !== "number" ||
typeof obj.has !== "function" ||
typeof obj.keys !== "function"
) {
throw new TypeError("Argument must be set-like");
}
}Call assertSetLike(b) at the top of each helper. Old runtimes then reject arrays the same way new ones do.
Choosing an Approach
| Situation | Approach |
|---|---|
| You target only current browsers or Node 22+ | Call the native methods directly |
| A few call sites must run on old runtimes | Helper functions with feature detection |
| Many call sites, or third-party code needs the methods | A spec-compliant polyfill such as core-js |
Exercise
On Node 20, union(new Set([1]), [2, 3]) with the helper above returns a Set of 1, 2, 3. What happens on Node 22, and why?
Show answer
It throws a TypeError. On Node 22 the helper calls the native union, which requires a set-like argument with a numeric size and has and keys methods. Arrays have no size property, so the check fails. The fallback did not catch this because spread accepts any iterable. Wrap the array with new Set([2, 3]), or add an assertSetLike check so both runtimes behave the same.
Exercise
Write a fallback for isDisjointFrom(a, b) that uses the native method when available.
Show answer
function isDisjointFrom(a, b) { return typeof a.isDisjointFrom === "function" ? a.isDisjointFrom(b) : [...a].every((x) => !b.has(x)); }