Sub topic 3
Objects in Sets: Reference Equality
This chapter assumes you know how to create and iterate a Set, use spread syntax with Array.prototype.filter, and tell object references apart from primitive values.
Objects in Sets: Reference Equality
Set methods compare elements with SameValueZero. Primitives match by value. Objects match only when both sides hold the same reference.
const x = { id: 1 };
const a = new Set([x, { id: 2 }]);
const b = new Set([x, { id: 2 }]);
console.log(a.intersection(b).size); // 1 (only the shared reference x)The two { id: 2 } literals have the same contents, but they are separate objects. To compare by content, build sets of keys such as id values instead of objects.
What Order Do Results Come In?
union and symmetricDifference list the receiver's elements first, followed by the new elements from the argument. difference keeps the receiver's order. intersection is different. When the argument is smaller, the engine loops over the argument instead of the receiver, so the result follows the argument's order.
const a = new Set([1, 2, 3, 4]);
const b = new Set([4, 3]);
console.log([...a.intersection(b)]); // [4, 3]Passing Maps and Set-Like Objects
The argument must be set-like. That means it has a numeric size, a has method, and a keys method. A Map qualifies, and its keys are used. An array has no size property, so passing one throws a TypeError.
const tags = new Set(["a", "b"]);
const counts = new Map([["b", 1], ["c", 2]]);
console.log([...tags.intersection(counts)]); // ["b"]
try {
tags.union(["c"]);
} catch (err) {
console.log(err instanceof TypeError); // true
}To use an array, wrap it first: tags.union(new Set(["c"])).
Browser Support, Feature Detection, and Fallbacks
These methods arrived in ES2025. Older runtimes don't have them. Check Set.prototype before calling them, and fall back to spread and filter when they are missing:
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)));
}Putting Set Methods to Work
A permission check is a common use. A subset test answers "is access allowed?", and difference tells you what is missing:
const required = new Set(["read", "write", "delete"]);
const granted = new Set(["read", "write"]);
console.log(required.isSubsetOf(granted)); // false
console.log([...required.difference(granted)]); // ["delete"]Exercise
You call new Set([{ id: 1 }]).intersection(new Set([{ id: 1 }])). What is the size of the result, and how would you compare the sets by id instead?
Show answer
The size is 0, because the two objects are different references. Map each set to its ids first, for example new Set([...setA].map((o) => o.id)), and then intersect those sets of ids.
Exercise
Why does new Set([1]).difference([1]) throw?
Show answer
An array is not set-like because it has no size property, so the method throws a TypeError. Pass new Set([1]) instead.