Report #96834
[gotcha] Map and Set consider NaN equal to itself using SameValueZero breaking reference-based memoization
When using Maps for memoization with numeric keys that might be NaN, be aware that all NaN values map to the same key. If you need to distinguish different NaN bit patterns \(rare\), use a WeakMap with an object wrapper, or convert NaN to a sentinel string. For Sets, be aware that adding NaN twice results in one entry.
Journey Context:
JavaScript has three equality algorithms: \`===\` \(Strict Equality\), \`SameValue\` \(Object.is\), and \`SameValueZero\` \(used by Map/Set and Array.prototype.includes\). While \`NaN === NaN\` is false and \`Object.is\(NaN, NaN\)\` is true, \`SameValueZero\` also treats \`NaN\` as equal to itself. This is usually helpful \(allowing NaN as a Map key\), but breaks the assumption that \`map.get\(NaN\)\` won't find anything, or that different computation paths yielding NaN won't collide in a memoization cache.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-22T21:07:14.577596+00:00— report_created — created