Set, Map, WeakSet and WeakMap in JavaScript
JavaScript provides built-in data structures that make working with collections more convenient and efficient. Let's examine Set, Map, WeakSet and WeakMap.
Set
Set is a collection of unique values where each value can appear only once.
Set Features
- Stores only unique values.
- Values can be of any type.
- Unordered structure.
Main Set Methods
add(value)— adds value.delete(value)— removes value.has(value)— checks value presence.clear()— clears collection.size— returns number of elements.
Set Usage Example
const set = new Set();
set.add(1);
set.add(2);
set.add(2); // Ignored, because 2 is already in collection
console.log(set.has(1)); // true
console.log(set.size); // 2
set.delete(1);
console.log(set); // Set { 2 }
Map
Map is a collection of key-value pairs where keys can be of any type.
Map Features
- Ordered structure.
- Allows using objects as keys.
Main Map Methods
set(key, value)— adds key-value pair.get(key)— returns value by key.delete(key)— removes pair by key.has(key)— checks key presence.clear()— clears collection.size— returns number of elements.
Map Usage Example
const map = new Map();
map.set('name', 'John');
map.set({ id: 1 }, 'Object');
console.log(map.get('name')); // John
console.log(map.size); // 2
map.delete('name');
console.log(map); // Map { { id: 1 } => 'Object' }
WeakSet
WeakSet is a collection that stores only objects. These objects are weakly referenced, allowing garbage collector to remove them if there are no other references.
WeakSet Features
- Stores only objects.
- Cannot get size or iterate elements.
- Used for temporary object storage.
Main WeakSet Methods
add(value)— adds object.delete(value)— removes object.has(value)— checks object presence.
WeakSet Usage Example
const weakSet = new WeakSet();
let obj = { name: 'John' };
weakSet.add(obj);
console.log(weakSet.has(obj)); // true
obj = null; // Object will be removed from WeakSet by garbage collector
WeakMap
WeakMap is a collection of key-value pairs where keys can only be objects and values can be anything. Keys are weakly referenced, allowing garbage collector to remove objects if there are no more references.
WeakMap Features
- Keys can only be objects.
- Values can be anything.
- Elements are weakly referenced, meaning automatic removal from collection if key is unreachable.
- Cannot iterate collection elements or get its size.
Main WeakMap Methods
set(key, value)— adds key-value pair.get(key)— returns value by key.delete(key)— removes key-value pair.has(key)— checks key presence.
WeakMap Usage Example
const weakMap = new WeakMap();
// Create object
let user = { name: "John" };
// Add object as key and value
weakMap.set(user, "User data");
console.log(weakMap.get(user)); // "User data"
// Remove reference to object
user = null;
// Object will be removed from WeakMap by garbage collector
WeakMap Limitations:
WeakMap is suitable for storing data associated with objects that can be removed by garbage collector. For example, for attaching metadata to DOM objects.