Why not a plain object?
What makes an object a poor dictionary, and why Map and Set were added to the language.
Map and Set. Why? Is {} that bad? As a dictionary — yes, and I have four pieces of evidence.Exhibit one: object keys are always strings. Whatever you put in the square brackets, the object turns it into a string. The number 1 becomes "1", so cage number 1 collides with a cat named "1". And an object used as a key turns into the string "[object Object]" — the same one for every object.
const cages = {};
cages[1] = "Mittens";
cages["1"] = "Tiger";
console.log(Object.keys(cages));
const mittens = { name: "Mittens" };
const tiger = { name: "Tiger" };
const seats = {};
seats[mittens] = "row 1";
seats[tiger] = "row 2";
console.log(seats[mittens]);
console.log(Object.keys(seats));
const map = new Map([[1, "Mittens"], ["1", "Tiger"]]);
console.log(map.size);"1", then via "[object Object]". A Map keeps 1 and "1" apart.Exhibit two: an object isn't empty even when it's empty. Every object inherits properties from a shared "ancestor": constructor, toString, __proto__. If your keys come from users — cat names, words, logins — sooner or later someone will be called constructor.
const counts = {};
const words = ["meow", "constructor", "meow"];
for (const w of words) {
counts[w] = (counts[w] ?? 0) + 1;
}
console.log(counts.meow);
console.log(counts.constructor);
console.log("toString" in {});1 — a function glued to a string. A Map has no such built-in keys: new Map().has("constructor") is false.Exhibit three: size and order. An object has no size — you build an array of keys every time: Object.keys(obj).length. And objects have their own idea of key order: number-like keys get moved to the front and sorted. Map and Set faithfully remember insertion order — for any keys.
const queue = { Tiger: "cage 5", 2: "VIP", Mittens: "cage 3", 1: "VIP" };
console.log(Object.keys(queue));
const line = new Map([["Tiger", 5], [2, "VIP"], ["Mittens", 3], [1, "VIP"]]);
console.log([...line.keys()]);"1" and "2" first. The Map kept the queue as it was.Exhibit four: speed. array.includes(x) checks elements one by one: the longer the array, the slower — that's O(n). Set and Map are built as hash tables: the value itself tells them where to look, so has runs in O(1) on average — just as fast for 10 guests as for a million.
Picture it: 10,000 visitors, and for each one you check whether they're on a list of 10,000 tickets. With an array — up to 100 million comparisons. With a Set — about 10,000 quick lookups.
Does this mean objects are obsolete? No. An object is still better when the fields are known in advance: a cat's passport { name, age, breed } is a record, not a dictionary. And objects speak JSON, while a Map doesn't: JSON.stringify(new Map([["a", 1]])) gives "{}". Before sending a Map to a server, turn it into an object with Object.fromEntries.
Map when keys come from outside, there are many of them, they aren't strings, or order matters. Set when you need "been here or not?". And yes, a cat named constructor did sign up for my show. I was ready.Myth or fact?
Decide whether the statement is true. Got it wrong? Try again; once you're right, Byte explains what's going on.