Sub topic 4
How Keys Are Compared: SameValueZero
This chapter assumes you know the basic Map and Set APIs from the previous two chapters. It also assumes you understand the difference between primitive values and object references. Every time a Map looks up a key, or a Set checks whether a value is already stored, it compares two values. The comparison rule is called SameValueZero. You need to know how it works to predict when two keys count as the same entry.
The three equality rules
JavaScript has several equality algorithms. SameValueZero sits between strict equality (===) and Object.is (which uses the SameValue algorithm).
| Comparison | `===` | SameValueZero (Map, Set) | `Object.is` |
|---|---|---|---|
| `NaN` vs `NaN` | false | true | true |
| `0` vs `-0` | true | true | false |
| `'1'` vs `1` | false | false | false |
| `{}` vs `{}` (two literals) | false | false | false |
SameValueZero behaves like === with one exception: NaN equals itself. Unlike Object.is, it also treats 0 and -0 as equal. It never converts types, so the string '1' and the number 1 are different keys.
Seeing it in action
const values = new Set([NaN, NaN, 0, -0, '1', 1]);
console.log(values.size); // 4
console.log(values.has(NaN)); // true
const m = new Map();
m.set(-0, 'zero');
console.log(m.get(0)); // 'zero'
console.log(new Set([{}, {}]).size); // 2The Set keeps four entries: NaN, 0, '1', and 1. The duplicate NaN and the -0 are dropped because they match values that are already stored. The two {} literals are separate objects, so both stay in their Set.
Maps and Sets also convert -0 to +0 when they store it:
const m = new Map([[-0, 'a']]);
const [storedKey] = m.keys();
console.log(Object.is(storedKey, -0)); // false
console.log(Object.is(storedKey, 0)); // trueCommon mistakes
Expecting structural equality for objects. Keys that are objects or arrays match only by reference. This lookup fails:
const points = new Map();
points.set([1, 2], 'A');
console.log(points.get([1, 2])); // undefinedThe array in get is a new array, so it is a different reference from the key. To look up by contents, keep a reference to the original array, or build a primitive key such as the string '1,2'.
Expecting type coercion. Plain objects turn property keys into strings, so obj[1] and obj['1'] point to the same property. A Map keeps them as two separate keys.
Exercise
What is the size of new Set([0, -0, NaN, Number('abc'), '0', [0], [0]]), and why?
Show answer
The size is 5. 0 and -0 are equal under SameValueZero, so they make one entry. NaN and Number('abc') (which is also NaN) make one entry. The string '0' is a separate entry because no type conversion happens. The two [0] arrays are different references, so each one is stored.
Exercise
A Map is created with map.set({ id: 7 }, 'user'). Why does map.get({ id: 7 }) return undefined, and how would you fix the lookup?
Show answer
Objects are compared by reference, and the object passed to get is a new object. Store the key object in a variable and pass that same variable to both set and get, or use a primitive key such as 7.