AceDevHub
Intermediate JavaScript Interview QuestionsIntermediateComparison

JavaScript · Question 62

When should you use Map instead of a plain Object in JavaScript?

Direct answer

Use Map for a dynamic key-value collection—especially when keys are not just strings/symbols, frequent iteration is central, or explicit size/collection APIs help; use plain objects for record-like structured data with known property names.

  • Map accepts keys of any value type, exposes size, has direct get/set/has/delete methods, and is directly iterable in insertion order.
  • Object uses string and symbol property keys and naturally models structured records that interact with object syntax, destructuring, JSON-oriented data, and prototypes.
  • A normal object inherits from a prototype, so arbitrary dictionary keys can collide with inherited names unless you check own properties or use Object.create(null).
  • Do not claim Map is always faster. Performance depends on workload and engine; choose based on semantics first and measure hot paths.

A user profile with fixed fields such as {id,name,email} is naturally an object. A cache keyed by request objects or IDs with frequent insert/delete/iteration may be a better Map.