Skip to content

Map vs Object

Both Map and Object store key-value pairs, but they have significant differences in key types, iteration, and performance.

FeatureMapObject
Key typesAny (primitives, objects, functions)String or Symbol only
OrderInsertion orderInteger keys first, then insertion
Size.size propertyManual (Object.keys().length)
IterationDirectly iterable (for...of)Needs Object.keys/values/entries
PrototypeNone (no inherited keys)Has Object.prototype
JSONNo native serializationJSON.stringify() works
PerformanceBetter with frequent add/deleteBetter for property access
// ✅ Map for dynamic key collections
const userRoles = new Map();
userRoles.set(user1, 'admin');
userRoles.set(user2, 'user');
userRoles.set(user3, 'editor');
// ✅ Map for frequent add/delete
const 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 types
const mixed = new Map();
mixed.set(1, 'number');
mixed.set('1', 'string');
mixed.set(true, 'boolean');
mixed.set({ obj: true }, 'object');
// ✅ Object for fixed known keys
const config = {
theme: 'dark',
lang: 'en',
debug: false
};
// ✅ Object when JSON serialization is needed
const data = { id: 1, name: 'Alice' };
const json = JSON.stringify(data);
// ✅ Object for a simple record/struct
const point = { x: 10, y: 20 };
// Object: keys are coerced to strings
const obj = {};
obj[42] = 'number';
obj['42'] = 'string'; // overwrites!
console.log(obj); // { '42': 'string' }
// Map: keys retain their type
const map = new Map();
map.set(42, 'number');
map.set('42', 'string');
console.log(map.get(42)); // 'number'
console.log(map.get('42')); // 'string'
  • 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