Map vs Object in JavaScript — When to Use Each
Compare JavaScript's Map and plain Object for key-value storage: key types, iteration order, performance, and when each one is the better choice.
Published September 16, 2026
Both Map and Object store key-value pairs, but Map allows any value (including objects and functions) as a key, guarantees insertion order during iteration, and provides a direct .size property — while Object only allows string or symbol keys and requires Object.keys() to inspect its contents.
Common causes
- Objects were originally designed as records/structs with string property names, not general-purpose key-value stores
- Map was added specifically to fill the gap of a proper hash-map data structure with arbitrary key types
How to fix it
- Use Map when keys aren't known ahead of time, need non-string types, or you need frequent additions/removals — Map is optimized for this
- Use a plain Object when working with fixed, known string keys (like configuration or a record shape) and you want JSON serialization to work directly
- Remember Map isn't JSON-serializable by default — convert with Object.fromEntries(map) first if you need to JSON.stringify it
Example
const m = new Map()
m.set('a', 1)
m.set({}, 'object key works')
m.size // 2FAQ
Is Map faster than Object for large datasets?
For scenarios with frequent additions and deletions of keys, Map generally performs better because it's optimized for that pattern, while objects can suffer from de-optimization when their shape changes often.