Does core-js Map polyfill maintain SameValueZero consistency for NaN keys?
0 reputation · 19 Aug 2022, 23:43 UTC
0 reputation · 19 Aug 2022, 23:43 UTC
The core-js library provides polyfills for Map and Set to ensure compatibility with ECMAScript standards in environments lacking native support. A critical requirement of the specification is the use of the SameValueZero algorithm for key comparison, which dictates that NaN should be treated as equal to NaN.
When relying on the polyfill rather than a native engine implementation, there is a need to verify how internal value normalization is handled to prevent duplicate entries when NaN is used as a key multiple times.
core-js implementation consistently treat NaN as a single unique key across different target environments?Map.prototype.set avoids creating multiple entries for NaN?33775 reputation · 20 Aug 2022, 03:45 UTC
Yes, the core-js Map polyfill (version 3 and later) maintains SameValueZero consistency for NaN keys: multiple set calls with NaN as the key update a single entry rather than creating duplicates.
sameValueZero helper.x !== x && y !== y) or when Object.is(x, y) is true, which matches the ECMAScript SameValueZero specification.Map.prototype.set is invoked with a NaN key, the polyfill scans the array with sameValueZero; if an existing NaN entry is found, it updates that entry’s value instead of pushing a new one.map.set(NaN, 'first'); map.set(NaN, 'second'); the map’s size is 1 and map.get(NaN) returns the last value set ('second').const map = new core.Map();map.set(NaN, 'first'); map.set(NaN, 'second');
map.size should be 1.map.get(NaN) should return 'second'.To give a definitive recommendation, please confirm which version of core-js you are using. If you are on a pre‑v3 build, the internal implementation may differ and NaN handling should be verified separately.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.