Curriculum
Concept 62 // Module 07: Collections, Iteration & Modules
Map & Set Collections: Composition, Union & Intersection
Module 07: Collections, Iteration & Modules•Concept 62 of 100• 5 min read
Map & Set Collections: Composition, Union & Intersection
**Map** and **Set** (ES6) are specialized collection objects. **Map** stores key-value pairs (where keys can be any type), and **Set** stores unique values. They offer better performance and features than standard Objects and Arrays for specific tasks.
The Core Concept
Syntax Reference
// 1. Set (Unique Values)
const set = new Set([1, 2, 2, 3]); // {1, 2, 3}
set.add(4);
set.has(2); // true
// 2. Map (Key-Value)
const map = new Map();
map.set('a', 1);
map.set({id: 1}, 'user'); // Object as key!
map.get('a'); // 1
// 3. WeakMap / WeakSet
const wm = new WeakMap(); // Keys MUST be objects
Live Interactive Studio
Converting an array to a `Set` and back to an array is the fastest way to remove duplicates in modern JavaScript.
JavaScript Editor
Loading...
Console Output
Waiting for output...
Key Code Patterns in Action
01
Removing Duplicates
// 1. Removing Duplicates
const raw = [1, 5, 2, 1, 5, 3];
const unique = [...new Set(raw)];
console.log(unique); // [1, 5, 2, 3]Converting an array to a `Set` and back to an array is the fastest way to remove duplicates in modern JavaScript.
02
Objects as Map Keys
// 2. Objects as Map Keys
const user = { name: "Subhajit" };
const metadata = new Map();
metadata.set(user, { lastLogin: "2025-12-17" });
console.log(metadata.get(user)); // { lastLogin: ... }In a regular Object, keys are converted to strings. A `Map` allows you to use actual objects as keys, which is powerful for associating metadata without modifying the original object.
03
WeakMap for Private Data
// 3. WeakMap for Private Data
const privates = new WeakMap();
class User {
constructor(id) {
privates.set(this, { id });
}
getId() {
return privates.get(this).id;
}
}
const me = new User(42);
console.log(me.getId()); // 42`WeakMap` doesn't prevent its keys from being **Garbage Collected**. If the `User` instance is deleted, the private data in the `WeakMap` is automatically removed from memory.
04
1. Core Usage: Map & Set Collections
// Core syntax and usage for Map & Set Collections: Composition, Union & Intersection
const feature = 'map-set-collections-composition-union-intersection';
console.log('Concept 62:', feature);
console.log('Active:', true);Illustrates the fundamental invocation and syntax structure for Map & Set Collections: Composition, Union & Intersection.
05
2. Defensive Handling & Edge Case
// Edge-case defensive handling
function handleSafe(input) {
if (input === null || input === undefined) return 'safe-fallback';
return String(input).trim();
}
console.log(handleSafe('map-set-collections-composition-union-intersection'));
console.log(handleSafe(null));Shows how to write defensive, safe JavaScript code that handles nullish or unexpected inputs when working with Map & Set Collections: Composition, Union & Intersection.
06
3. Clean Architecture & Production Pattern
// Production-grade pattern for Map & Set Collections: Composition, Union & Intersection
const settings = Object.freeze({
id: 'map-set-collections-composition-union-intersection',
ready: true,
timestamp: Date.now()
});
console.log('Settings initialized:', settings.id, settings.ready);Demonstrates clean, immutable production code avoiding unintended state mutations.
Real-World Engineering Takeaways
Set: Maintaining a list of active users, selected IDs, or unique tags.
Map: High-performance lookups, caching results (memoization), or mapping UI elements to state objects.
WeakMap: Storing truly private data or metadata for DOM elements in a way that avoids memory leaks.
WeakSet: Tracking 'visited' objects in recursive algorithms.
Under the Hood: Engine & Memory Mechanics
How JavaScript Executes This
### **Hash Table Internals**
**1. Hash Maps:**
- `Map` and `Set` use **Hash Tables** in the **Heap** for O(1) average time complexity for access.
- Standard Objects also use hash tables, but `Map` is optimized for frequent additions/removals.
**2. Garbage Collection (Weak Variants):**
- **Strong Reference:** A standard `Map` keeps its keys alive in the **RAM** as long as the map exists.
- **Weak Reference:** A `WeakMap` holds a "weak" reference. If no other code points to the key object, the **Garbage Collector** can reclaim that memory even if it's still in the map.
**3. Memory Overhead:**
- Each `Map` entry requires more memory than a standard Object property due to the complexity of the Hash Table structure.
Quick Knowledge Check
Test your understanding on these 10 questions (one by one):
Score: 0 / 10
Question 1 of 10 • Core Purpose & Mental Model
What is the core purpose of Map & Set Collections: Composition, Union & Intersection in JavaScript?
// Concept 62: Map & Set Collections: Composition, Union & Intersection
// Understanding the core mental model1 / 10