JSworkinges2015

Map and WeakMap

Keyed data, done right.

A plain object pretends to be a dictionary, but its keys are always strings and it carries inherited baggage. Map is the real thing: any value as a key, honest size, guaranteed insertion order — and WeakMap attaches private metadata without leaking memory.

Object keys vs Map keys

JSintro
Press Run to execute

The object stores only one entry (Object.keys(obj).length is 1) because the numeric key 1 was coerced to the string '1' and overwrote it, while the Map keeps both as distinct keys and reports size 2 — proof that Map preserves key type.