CodeOath
← All posts
JavaScript65 min total · 16 parts

JavaScript Array and Object Methods Cheat Sheet

Contents — Part 12 of 16: Map and Set: Alternatives to Plain Objects
Part 12 of 16 · ~1 min

Map and Set: Alternatives to Plain Objects

A plain object works as a key-value store for simple cases, but Map and Set solve specific limitations objects have.

const map = new Map();
map.set("a", 1);
map.set({ id: 1 }, "object key"); // Map allows ANY value as a key, not just strings/symbols
map.get("a"); // 1
map.has("a"); // true
map.size;     // 2 — a real property, not a method call like Object.keys(obj).length

for (const [key, value] of map) { // Map is directly iterable, in INSERTION order
  console.log(key, value);
}
Plain objectMap
Key typesString or Symbol only (numbers get coerced to strings)Any value, including objects and functions
SizeObject.keys(obj).length.size, a direct property
Iteration orderInsertion order for string keys, but integer-like keys are always sorted numerically firstAlways strict insertion order
Iterable directlyNo — needs Object.entries() firstYes — for...of works directly
Keys colliding with inherited namesPossible — a key like "constructor" or "hasOwnProperty" shares a name with something on Object.prototype, which can confuse a naive for...in loop or a check like obj.hasOwnProperty if it was itself overwrittenNot possible — a Map's keys are just data, with no prototype chain involved

Set stores unique values (no duplicates, checked with the same SameValueZero algorithm .includes() uses) and is the standard tool for de-duplicating an array:

const numbers = [1, 2, 2, 3, 3, 3];
const unique = [...new Set(numbers)]; // [1, 2, 3] — spread a Set back into an array