Object keys vs Map keys
Predict the lookup results and the Map size.
Code Snippet
const first = { id: 1 }, second = { id: 1 };
const dictionary = { [first]: "A", [second]: "B" };
const map = new Map([[first, "A"], [second, "B"], [NaN, "C"]]);
console.log(dictionary[first], Object.keys(dictionary).length);
console.log(map.get(first), map.get({ id: 1 }), map.get(NaN), map.size);Expected Output & Explanation
B 1
A undefined C 3 Both ordinary objects convert to the same property key string here. Map compares objects by identity, but it treats NaN as equal to NaN for key lookup.
Module Context & References
JavaScript · Language output drills Trace closures, receivers, prototypes, coercion and array edge cases
Personal Notes · Markdown