Set.prototype.isSupersetOf()
isSupersetOf(other) Set.prototype.isSupersetOf() returns true when every element of another Set or set-like object is also present in the receiver, and false otherwise. It is the boolean counterpart to Set.prototype.intersection(): where intersection() answers “what do they share?”, isSupersetOf() answers “does the receiver fully contain other?”. It ships with the ES2025 batch of new Set methods (union, intersection, difference, symmetricDifference, isSubsetOf, isDisjointFrom).
Syntax
Set.prototype.isSupersetOf() is called on a Set instance and returns a primitive boolean describing whether the receiver is a superset of the argument:
setInstance.isSupersetOf(other)
Parameters
other— ASetinstance, or any set-like object exposing ahasmethod and asizeproperty. Arrays do not qualify on their own; wrap them innew Set(arr)first.
Return value
A primitive boolean. true if every element of other is also in the receiver; false otherwise. Equal sets return true in both directions, because isSupersetOf is the inclusive superset relation, not the proper superset relation.
Description
In math notation, the result is A ⊇ B = { x | x ∈ B → x ∈ A }. The JavaScript implementation matches that definition, with one wrinkle: the receiver must be a real Set instance (the spec reads its internal [[SetData]] slot directly), while the argument only needs a has method and a size property. Because the method never iterates the receiver, no keys() is required on either side, which separates it from Set.prototype.union() and the other set-producing methods.
Membership uses SameValueZero, the same equality Set uses internally, so NaN equals NaN and +0 equals -0. The method ships in Baseline 2024 and is part of the new Set methods proposal by Michał Wadas.
A short, useful algorithm to keep in mind:
- If the receiver has fewer elements than
other, returnfalseimmediately. This is a cheap fast-path that skips every membership probe. - Otherwise walk
otherin iteration order and returnfalseon the first element that the receiver rejects. - If every probe passes, return
true.
// Algorithm in action: the first failing probe short-circuits the rest
const rhs = new Set([1, 2, 3, 4]);
const lhs = new Set([2, 4, 6, 8]);
console.log(lhs.isSupersetOf(rhs));
// false (probe of `1` fails, the rest of `rhs` is never tested)
Two practical consequences follow:
- Equal sets are supersets of each other. If you specifically need proper supersetting (strict containment), combine the call with a size check.
- An empty argument is contained by anything. Vacuous truth. The empty set qualifies against every set, including itself. Handy as a default branch in permission and feature checks.
isSupersetOf does not mutate either input and allocates no result Set, because the answer is a primitive boolean. That makes it cheap to call inside hot paths, render functions, and reducer pipelines where allocating a new collection would be wasteful.
// Cheap membership gate in a hot path
function isUniverse(workingSet, candidate) {
return workingSet.isSupersetOf(candidate);
}
const allowed = new Set(["read", "write", "delete", "admin"]);
const requested = new Set(["read", "write"]);
console.log(isUniverse(allowed, requested));
// true
Examples
Test the superset relation
The common case is two real Set instances. The result is a primitive boolean, not a Set. The examples below start with the canonical math-superset question, then move into the equal-sets edge case, the set-like argument contract, and the brand check that catches a duck-typed receiver:
const evens = new Set([2, 4, 6, 8, 10, 12, 14, 16, 18]);
const fours = new Set([4, 8, 12, 16]);
console.log(evens.isSupersetOf(fours));
// true
console.log(fours.isSupersetOf(evens));
// false
Every element of fours (4, 8, 12, 16) appears in evens, so the first call returns true. Going the other way returns false because fours also lacks 2, 6, 10, 14, 18, which all live in evens.
Equal sets are supersets of each other
isSupersetOf reports the inclusive superset relation, so two equal sets return true in both directions. If you need the strict variant, gate on size:
const set1 = new Set([1, 2, 3]);
const set2 = new Set([1, 2, 3]);
console.log(set1.isSupersetOf(set2)); // true
console.log(set2.isSupersetOf(set1)); // true
const isProperSuperset = (a, b) => a.isSupersetOf(b) && a.size > b.size;
console.log(isProperSuperset(set1, set2));
// false
Pass a set-like argument
The argument does not have to be a real Set. Any object that exposes has and size is accepted, which is useful when integrating with a library that already maintains a custom membership collection:
const whitelist = {
size: 4,
has(value) {
return value === "a" || value === "b" || value === "c" || value === "d";
},
};
console.log(new Set(["a", "b", "c", "d"]).isSupersetOf(whitelist));
// true
Note that keys() is not required here, because isSupersetOf only ever iterates the argument. That makes it slightly more permissive than Set.prototype.union(), which needs keys() when the set-like ends up on the iterated side.
NaN is treated as equal to itself
SameValueZero equality means NaN matches NaN. The result is a Set that contains NaN on both sides, so the membership probe succeeds:
console.log(new Set([NaN, 1, 2, 3]).isSupersetOf(new Set([NaN, 2])));
// true
This differs from === but matches the behavior of every other Set operation, so values that round-trip through a Set are safe to use with isSupersetOf without any extra Number.isNaN dance.
The receiver must be a real Set
Set.prototype.isSupersetOf reads the receiver’s internal [[SetData]] slot directly, so plain objects that only look like a set throw a brand-check error. This is the same guard the spec applies to every built-in Set.prototype method, so a duck-typed shim is not interchangeable with a real Set:
const fakeSet = {
size: 2,
has: () => true,
keys: function* () { yield 1; yield 2; },
};
try {
fakeSet.isSupersetOf(new Set([1, 2, 3]));
} catch (err) {
console.log(err.constructor.name);
// TypeError
}
If you need to ask the superset question the other way around, flip the operands. A real Set becomes the receiver, the brand check passes, and the set-like only has to satisfy the has / size contract on the argument side:
const real = new Set([1, 2, 3, 4, 5]);
const fake = { size: 2, has: (v) => v === 1 || v === 2 };
console.log(real.isSupersetOf(fake));
// true
Any set is a superset of the empty set
Vacuous truth: the empty set satisfies the superset relation against any Set, including another empty Set. Use it as a default branch in feature-flag and permission checks:
console.log(new Set([1, 2, 3]).isSupersetOf(new Set()));
// true
console.log(new Set().isSupersetOf(new Set()));
// true
Set.prototype.isSupersetOf() performance
Cost is O(min(n, m)) when both inputs are real Set instances, where n is the receiver’s size and m is the argument’s size. The size fast-path returns false immediately when n < m, so handing the method a large argument and a small receiver is the case it is tuned for. Each membership probe against a real Set is amortized O(1), so total work is bounded by the size of the smaller side.
// Fast-path: 2 < 10 short-circuits without probing
const small = new Set([1, 2]);
const big = new Set([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
console.log(small.isSupersetOf(big));
// false
Allocations: nothing for the result (a primitive boolean), no intermediate array, no result Set. That makes it cheaper than intersection() for the same logical question: a [...other].every(receiver.has) style fallback allocates an intermediate array and pays an extra O(m) copy. If you find yourself reaching for Array.prototype.every and a has callback, swap to isSupersetOf and skip the array materialization.
Guards benefit from the same fast-path. Wrapping an expensive membership walk in if (universe.isSupersetOf(candidate)) returns false before probing any element when the candidate is larger than the universe, which beats a hand-rolled for loop in most realistic shapes.
Specifications
Set.prototype.isSupersetOf() is defined in ECMA-262 § 24.2.4.12, part of the 16th edition (ECMAScript 2025). The spec text is short and worth reading: it states that the receiver is a real Set instance, that the argument is read through a Set Record (so it only needs has and size), and that membership uses SameValueZero equality. The full proposal text and meeting notes live in the tc39/proposal-new-set-methods repository.
Browser support
Set.prototype.isSupersetOf() is part of the new Set methods proposal by Michał Wadas, which graduated to Stage 4 in 2023 and ships with the ECMAScript 2025 specification. Support reached Baseline 2024 in June 2024:
| Engine | First version |
|---|---|
| Chrome / Edge | 122 (Feb 2024) |
| Firefox | 127 (Jun 2024) |
| Safari | 17.0 (Sep 2023) |
| Node.js | 22.0.0 |
For older environments, both core-js and es-shims provide a polyfill. core-js ships a broad collection-level patch, while es-shims offers a smaller, focused polyfill that covers the new Set methods individually and follows the spec closely.
Polyfill pattern
If you cannot pull in a package, the spec algorithm fits in a handful of lines. This is the same shape the polyfills above use internally, written in plain JavaScript so it works on any runtime that has Set:
function isSupersetOf(a, b) {
if (a.size < b.size) return false;
for (const v of b) if (!a.has(v)) return false;
return true;
}
console.log(isSupersetOf(new Set([1, 2, 3]), new Set([1, 2])));
// true
The size check is read from the receiver, then b is iterated while the receiver is queried through has. That asymmetry matches the built-in, with one caveat: this userland version accepts any receiver with a working for..of (size is read from the receiver, not required to be a real Set). The built-in does not iterate the receiver at all.