Sub topic 9
What Order Do Results Come In?
This chapter assumes you know that a Set iterates in insertion order and that you have already seen union, intersection, difference, and symmetricDifference in earlier chapters. The focus here is narrower: when you spread a result into an array or loop over it, which element comes first?
Each method follows a fixed rule, so the order is predictable. The rules are not all the same, though, and one of them depends on the sizes of the two sets.
The Rules at a Glance
| Method | Order of the result |
|---|---|
| `a.union(b)` | All of `a` in `a`'s order, then new elements of `b` in `b`'s order |
| `a.intersection(b)` | The smaller set's order; on a size tie, `a`'s order |
| `a.difference(b)` | Always `a`'s order |
| `a.symmetricDifference(b)` | Remaining elements of `a` in `a`'s order, then new elements of `b` in `b`'s order |
union: Receiver First, Then Newcomers
union copies the receiver (the set you call the method on), then walks the argument's keys and adds any element it has not seen yet. Re-adding an existing value does not move it, so shared elements keep their position from a.
const a = new Set(["c", "a"]);
const b = new Set(["b", "a", "d"]);
console.log([...a.union(b)]); // ["c", "a", "b", "d"]
console.log([...b.union(a)]); // ["b", "a", "d", "c"]Both results contain the same four elements. Only the order changes.
intersection: The Smaller Set Decides
For speed, intersection iterates whichever set is smaller and checks each element against the other set with has. The result is built in the order of that iteration. If the sizes are equal, it iterates the receiver.
This size rule causes the most common surprise:
const big = new Set([1, 2, 3, 4, 5]);
const small = new Set([5, 4, 3]);
console.log([...big.intersection(small)]); // [5, 4, 3] (small is iterated)
console.log([...small.intersection(big)]); // [5, 4, 3]
const x = new Set([1, 2, 3]);
const y = new Set([3, 2, 1]);
console.log([...x.intersection(y)]); // [1, 2, 3] (tie: receiver's order)
console.log([...y.intersection(x)]); // [3, 2, 1]You might expect big.intersection(small) to follow big's order, but it follows small's. If one set grows past the other at runtime, the order of your results can flip even though the code stays the same.
difference: Always the Receiver's Order
difference also switches strategy based on size. It may loop over the argument and delete matches from a copy of the receiver. Either way it starts from a copy of the receiver and only removes elements, so the survivors keep the receiver's order.
const a = new Set([4, 1, 3, 2]);
const b = new Set([3, 9, 8, 7, 6, 5]);
console.log([...a.difference(b)]); // [4, 1, 2]symmetricDifference: Receiver Leftovers, Then Argument Extras
symmetricDifference copies the receiver and then walks the argument. A shared element is removed. A new element is appended at the end.
const a = new Set([1, 2, 3]);
const b = new Set([5, 3, 4]);
console.log([...a.symmetricDifference(b)]); // [1, 2, 5, 4]Elements 1 and 2 keep a's order. Then 5 and 4 follow in the order they appear in b.
When the Argument Is a Map
If the argument is a Map, the "argument's order" means the order of its keys() iterator, which is the order the keys were first inserted.
const tags = new Set(["x"]);
const counts = new Map([["z", 1], ["y", 2]]);
console.log([...tags.union(counts)]); // ["x", "z", "y"]Practical Advice
- Mathematically, sets are unordered. Treat any result order as a side effect unless you have a reason to rely on it.
- For display, sort explicitly:
[...result].sort(). union,difference, andsymmetricDifferencealways put the receiver's elements first, so you can depend on their order.intersectionis the one method whose order depends on the sizes of the two sets.
Exercise
Given const p = new Set(["d", "b", "a", "c"]) and const q = new Set(["c", "a"]), what do [...p.intersection(q)] and [...p.difference(q)] print?
Show answer
p.intersection(q) prints ["c", "a"] because q is smaller, so q is iterated and its order is used. p.difference(q) prints ["d", "b"] because difference always keeps the receiver's order.
Exercise
With const a = new Set([1, 2]) and const b = new Set([2, 1]), why does [...a.intersection(b)] give [1, 2] while [...b.intersection(a)] gives [2, 1]?
Show answer
The sets are the same size. On a tie, intersection iterates the receiver, so each call returns the receiver's insertion order.