Map and WeakMap are both key-value collections, but they have important differences:
// Map — any keys
const map = new Map([[1, 'number'], ['a', 'string'], [{}, 'object']]);
// WeakMap — only objects as keys
const obj = {};
const weakMap = new WeakMap([[obj, 'value']]);Think of Map as a regular box with compartments, where each compartment has a label with any name. And WeakMap is a magical box that throws away compartments when no one remembers their labels anymore! 📦✨
// Map can use any keys
const map = new Map();
map.set(1, 'number'); // number ✅
map.set('a', 'string'); // string ✅
map.set({}, 'object'); // object ✅
// WeakMap only objects as keys
const weakMap = new WeakMap();
const keyObj = {};
// weakMap.set('string', 'value'); // ❌ Error!
weakMap.set(keyObj, 'value'); // ✅ Only objectsconst map = new Map([['a', 1], ['b', 2]]);
// Map can be iterated
for (const [key, value] of map) {
console.log(key, value); // 'a' 1, 'b' 2
}
const weakMap = new WeakMap([[{}, 'value']]);
// ❌ WeakMap cannot be iterated!
// for (const [key, value] of weakMap) // Error!WeakMap is like a secret safe that destroys documents when their owner forgets about them. If no one holds a reference to the key object anymore, WeakMap forgets the key-value pair automatically! 🔐
let obj = { id: 1 };
const weakMap = new WeakMap([[obj, 'private data']]);
// When obj is no longer used by anyone
obj = null;
// Pair will be automatically removed from WeakMap
// Garbage collector takes care of it 🗑️// Caching results
const cache = new Map();
function getData(key) {
if (cache.has(key)) {
return cache.get(key);
}
const result = fetchData(key);
cache.set(key, result);
return result;
}// Need to go through all pairs
const config = new Map([['theme', 'dark'], ['lang', 'en']]);
config.forEach((value, key) => {
console.log(`${key}: ${value}`);
});// Private user data
const privateData = new WeakMap();
class User {
constructor(name) {
this.name = name;
// Private data not visible from outside
privateData.set(this, { password: 'secret' });
}
getPassword() {
return privateData.get(this).password;
}
}// Cache that cleans itself
const cache = new WeakMap();
function process(obj) {
if (cache.has(obj)) {
return cache.get(obj); // from cache
}
const result = heavyCalculation(obj);
cache.set(obj, result); // to cache
return result;
}
// When obj object is no longer needed,
// cache will automatically clear// ❌ Error!
const weakMap = new WeakMap();
// weakMap.set('key', 'value'); // TypeError!
// weakMap.set(123, 'value'); // TypeError!
// ✅ Correct
const obj = {};
weakMap.set(obj, 'value'); // Only objects as keys!// ❌ Cannot iterate WeakMap
const weakMap = new WeakMap([[{}, 'value']]);
// weakMap.forEach((value, key) => {}); // Error!Understanding the difference between Map and WeakMap helps choose the right tool for the job and avoid memory issues! 💪
Want more articles to prepare for interviews? Subscribe to EasyAdvice, bookmark the site and improve yourself every day 💪