4 ms·
You can use objects and other things as keys for a Set or Map. As far as I know you can't do that for objects. const s = new Set(); const a = {}, b =
by postalrat 2y ago
You can use objects and other things as keys for a Set or Map. As far as I know you can't do that for objects.
const s = new Set();
const a = {}, b = {};
s.add(a);
s.has(a); // true
s.has(b); // false
const o = {};
o[a] = true;
o[a]; // true
o[b]; // true, cause its cast to a string like "[object Object]"
I've also seen Map perform faster than using an object as a Map.
- brobdingnag_pp 2y agoBeware of a gotcha here: object keys are tested for strict equality and not deep equality, in other words add/get must use the same object instance and not just an object with the same keys/values