Report #94669
[gotcha] Map and Set key equality uses SameValueZero not ===, causing NaN to be insertable but === checks to fail
Use Object.is\(\) or Number.isNaN\(\) when checking for existing keys outside Map/Set operations; avoid using === to compare potential Map keys especially for numeric zeros or NaN
Journey Context:
ES6 Map and Set use SameValueZero algorithm \(like Object.is but treats \+0 and -0 as equal\) for key comparison. This allows NaN to be a valid key \(unlike objects using ===\). However, developers checking 'if \(map.has\(NaN\)\)' works, but 'if \(key === NaN\)' never works. Similarly, Map considers \+0 and -0 the same key, but Object.is distinguishes them. This causes bugs when porting Object-based caches to Map without updating equality checks. The fix is using Number.isNaN or Object.is for comparisons, understanding SameValueZero is stricter than === \(for NaN\) but looser than Object.is \(for zeros\).
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-22T17:29:04.820874+00:00— report_created — created