Set.prototype.isDisjointFrom()

Returns true if this set shares no elements at all with the given set.

Signature
isDisjointFrom(other)
  • Added in JavaScript ES2025

Parameters

ParameterTypeDescription
$otherSet | set-like objectA Set, or any set-like object: one with a numeric size property and callable has() and keys() methods.

Return value

booleantrue if this set has no elements in common with the other set, and false otherwise.

Errors

Throws a TypeError if other is not set-like — missing a non-negative numeric size, or missing callable has() and keys() methods.

Changelog

  • PHP ES2025Added with the other Set composition methods; Baseline newly available since June 2024.

isDisjointFrom() tests whether the intersection of two sets is empty. It is the cheap way to write a.intersection(b).size === 0: no result set is allocated, and the scan stops at the first shared element.

How it chooses what to walk

The cost depends on the smaller collection. If this set is no larger than other, the method iterates this set and calls other.has(); otherwise it iterates other through its keys() method and calls this.has(). Either way it bails out as soon as a common element turns up, so a conflict found early costs almost nothing.

When to reach for it

Use it for conflict and safety checks: does a user's deny list overlap their allow list, do two scheduled jobs touch any of the same resources, does user input contain any reserved word. Phrasing these as "disjoint" keeps the happy path a single positive assertion instead of a length comparison on a throwaway set.

What trips people up

Empty sets are disjoint from everything, including themselves — an empty input silently passes the check, so validate emptiness separately if it matters. As with the other composition methods, arrays are not set-like and must be wrapped in new Set().

Example

Try it yourself
Loading editor…
Press Run to execute the code.

When to use it

  • Reject a request whose deny list overlaps its allow list
  • Check that two concurrent jobs claim no resources in common
  • Verify user-supplied identifiers avoid every reserved keyword
  • Confirm two feature cohorts share no participants before an A/B test

Frequently asked questions

Is isDisjointFrom faster than checking intersection().size?

Yes, in both memory and time: it allocates no result set and returns as soon as it finds one shared element, whereas intersection() builds a complete Set before you can measure it.

Are two empty sets disjoint?

Yes — an empty set has no elements to share, so it is disjoint from every set including another empty one. Check size separately if an empty input should be treated as an error.

Does the order of the two sets matter?

Not to the answer, which is symmetric: a.isDisjointFrom(b) always equals b.isDisjointFrom(a). It only affects which collection is iterated internally, and the engine already picks the smaller one.

More examples

Guarding against reserved names

The disjointness test is the validation; intersection() is only paid for on the failure path, where it names the offending field. The output shows the single reserved word that slipped into the schema.

Example · javascript
const reserved = new Set(["class", "new", "delete", "const"]);
const fields = new Set(["title", "body", "delete"]);

if (fields.isDisjointFrom(reserved)) {
  console.log("ok");
} else {
  console.log("reserved:", [...fields.intersection(reserved)]); // reserved: ["delete"]
}

Cheaper than measuring the intersection

Because the receiver is smaller, the method walks only its three elements and calls has() on the large set, returning at the very first match. Building big.intersection(small) would allocate a whole new Set to answer the same yes/no question.

Example · javascript
const big = new Set(Array.from({ length: 50_000 }, (_, i) => i));
const small = new Set([3, 7, 11]);

console.log(small.isDisjointFrom(big)); // false — stops at 3
console.log(new Set([-1, -2]).isDisjointFrom(big)); // true

Disjoint is not the same as different

The two sets are clearly not equal, yet they are not disjoint either: they share the element 3. Disjointness is about zero overlap, not about the sets being unequal — the symmetric difference being non-empty proves nothing either way.

Example · javascript
const a = new Set([1, 2, 3]);
const b = new Set([3, 4, 5]);

console.log(a.isDisjointFrom(b)); // false
console.log([...a.symmetricDifference(b)]); // [1, 2, 4, 5]
console.log([...a.intersection(b)]); // [3]

Discussion

  • Be the first to comment on this lesson.