Map and Set in JavaScript
As JavaScript evolved, developers needed better data structures than traditional objects and arrays for certain use cases.
Thatโs where Map and Set come in.
They provide more control, better performance in some cases, and cleaner semantics.
๐น What is a Map?
๐ A Map is a collection of key-value pairs, similar to objectsโbut with important differences.
๐ Example
const map = new Map();
map.set("name", "Sayantan");
map.set("age", 21);
console.log(map.get("name")); // Sayantan
๐น Key Features of Map
Keys can be any data type (not just strings)
Maintains insertion order
Has built-in methods like:
.set().get().has().delete()
๐น Example with Non-String Keys
const map = new Map();
map.set(1, "Number key");
map.set(true, "Boolean key");
console.log(map.get(1)); // Number key
๐ This is NOT possible with normal objects.
๐น What is a Set?
๐ A Set is a collection of unique values.
It automatically removes duplicates.
๐ Example
const set = new Set([1, 2, 2, 3, 4]);
console.log(set); // {1, 2, 3, 4}
๐น Key Features of Set
Stores only unique values
No duplicate entries
Maintains insertion order
Useful methods:
.add().has().delete()
๐น Example: Removing Duplicates
const arr = [1, 2, 2, 3];
const unique = [...new Set(arr)];
console.log(unique); // [1, 2, 3]
๐ฅ Map vs Object
| Feature | Map | Object |
|---|---|---|
| Key Types | Any type | Strings/Symbols only |
| Order | Maintains order | Not guaranteed (historically) |
| Iteration | Easy (for...of) |
Requires extra methods |
| Performance | Better for frequent updates | Good for simple use cases |
๐ Problem with Objects
const obj = {};
obj[1] = "Number key";
console.log(obj["1"]); // "Number key"
๐ Keys are converted to strings โ loss of flexibility
๐ฅ Set vs Array
| Feature | Set | Array |
|---|---|---|
| Duplicates | Not allowed | Allowed |
| Order | Maintained | Maintained |
| Access | No index | Index-based |
| Use Case | Unique values | Ordered data |
๐ Problem with Arrays
const arr = [1, 2, 2, 3];
const unique = arr.filter((item, index) => arr.indexOf(item) === index);
console.log(unique); // [1, 2, 3]
โ Complex โ Inefficient
โ Set solves this easily
๐น When to Use Map
Use Map when:
You need key-value storage
Keys are not strings
Frequent additions/deletions
Need guaranteed insertion order
๐ Real Example
const userRoles = new Map();
userRoles.set("Sayantan", "Admin");
userRoles.set("Rahul", "User");
console.log(userRoles.get("Sayantan"));
๐น When to Use Set
Use Set when:
You need unique values
Removing duplicates
Checking existence quickly
๐ Real Example
const visitedPages = new Set();
visitedPages.add("/home");
visitedPages.add("/about");
visitedPages.add("/home");
console.log(visitedPages); // no duplicates
๐ง Mental Model
Map โ advanced object (key-value storage)
Set โ advanced array (unique values)
๐ Practical Use Cases
1. Remove Duplicates
const nums = [1, 2, 2, 3];
const uniqueNums = [...new Set(nums)];
2. Fast Lookup
const set = new Set([1, 2, 3]);
console.log(set.has(2)); // true
3. Dynamic Key Storage
const map = new Map();
const objKey = { id: 1 };
map.set(objKey, "Data");
console.log(map.get(objKey)); // Data
โ ๏ธ Common Mistakes
โ Using Object Instead of Map
const obj = {};
obj[{ id: 1 }] = "value";
console.log(obj); // "[object Object]"
๐ Key gets stringified โ incorrect behavior
โ Expecting Index in Set
const set = new Set([10, 20, 30]);
console.log(set[0]); // undefined โ
๐ Sets donโt support indexing