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

Use a Map when keys are dynamic, can be any type, or you add and remove entries often. Use a plain object for fixed, string-keyed records such as configuration or JSON-like data.

The concept

Object keys are always strings or symbols; a number key becomes the string '1'. Map keys can be anything — objects, functions, numbers — and keep their type.

Map remembers insertion order, exposes .size directly, and is optimised for frequent additions and deletions. Objects inherit properties like toString from their prototype, which can collide with your keys.

Key types behave differently

const o = {}; o[1] = 'a'; o['1'] = 'b'
Object.keys(o) // ['1'] (same key)
const m = new Map(); m.set(1,'a'); m.set('1','b')
m.size // 2 (different keys)

Practical examples

Count word frequency

const counts = new Map()
for (const w of words) counts.set(w, (counts.get(w) ?? 0) + 1)

Object as key

Attach metadata to DOM nodes or objects without modifying them.

const meta = new Map()
meta.set(buttonEl, { clicks: 0 })

Plain object for a config

const config = { host: 'localhost', port: 3000 }
JSON.stringify(config) // works out of the box

Common mistakes

  • Using an object as a dictionary with user-provided keys, risking clashes with names such as __proto__.
  • Expecting JSON.stringify(map) to work — it returns {}. Convert with Object.fromEntries(map).
  • Using Map for tiny fixed shapes where an object literal is clearer.

Key takeaways

  • Map: any key type, ordered, .size, fast add/remove.
  • Object: simple, JSON-friendly, string/symbol keys.
  • Convert freely with new Map(Object.entries(o)) and Object.fromEntries(m).

FAQ

Is Map faster than an object?

For frequent adds and deletes of many keys, usually yes. For small static shapes the difference is irrelevant.

What is a WeakMap?

A Map whose object keys do not prevent garbage collection, useful for private data and caches.

More JavaScript articles