Map vs Object
Map vs Object
Section titled “Map vs Object”Introduction
Section titled “Introduction”Both Map and Object store key-value pairs, but they have significant differences in key types, iteration, and performance.
Comparison Table
Section titled “Comparison Table”| Feature | Map | Object |
|---|---|---|
| Key types | Any (primitives, objects, functions) | String or Symbol only |
| Order | Insertion order | Integer keys first, then insertion |
| Size | .size property | Manual (Object.keys().length) |
| Iteration | Directly iterable (for...of) | Needs Object.keys/values/entries |
| Prototype | None (no inherited keys) | Has Object.prototype |
| JSON | No native serialization | JSON.stringify() works |
| Performance | Better with frequent add/delete | Better for property access |
When to Use Map
Section titled “When to Use Map”// ✅ Map for dynamic key collectionsconst userRoles = new Map();userRoles.set(user1, 'admin');userRoles.set(user2, 'user');userRoles.set(user3, 'editor');
// ✅ Map for frequent add/deleteconst cache = new Map();function memoized(fn) { return function(key) { if (cache.has(key)) return cache.get(key); const result = fn(key); cache.set(key, result); return result; };}
// ✅ Map when keys are objects or mixed typesconst mixed = new Map();mixed.set(1, 'number');mixed.set('1', 'string');mixed.set(true, 'boolean');mixed.set({ obj: true }, 'object');When to Use Object
Section titled “When to Use Object”// ✅ Object for fixed known keysconst config = { theme: 'dark', lang: 'en', debug: false};
// ✅ Object when JSON serialization is neededconst data = { id: 1, name: 'Alice' };const json = JSON.stringify(data);
// ✅ Object for a simple record/structconst point = { x: 10, y: 20 };Key Type Demonstration
Section titled “Key Type Demonstration”// Object: keys are coerced to stringsconst obj = {};obj[42] = 'number';obj['42'] = 'string'; // overwrites!console.log(obj); // { '42': 'string' }
// Map: keys retain their typeconst map = new Map();map.set(42, 'number');map.set('42', 'string');console.log(map.get(42)); // 'number'console.log(map.get('42')); // 'string'Summary
Section titled “Summary”- Use Map for dynamic keys, object keys, frequent add/delete, and iteration
- Use Object for fixed keys, JSON serialization, and simple records
- Map has better performance for collections that change frequently
- Object is more natural for configuration and data transfer