WeakSet rounds out the Modern Collections section: a Set that holds only objects, weakly, with no iteration — the natural choice when you only need to mark or tag objects, not store any value alongside them.
1WeakSet | JavaScript Tutorial - In-Depth Guide Part 1
WeakSet is to Set what WeakMap is to Map: it stores only objects, holds them weakly, and allows those objects to be garbage collected when nothing else references them.
const processed = new WeakSet();
let item = { id: 1 };
processed.add(item);
processed.has(item); // trueSet + Weak References
2WeakSet | JavaScript Tutorial - In-Depth Guide Part 2
Like WeakMap, WeakSet only accepts objects — never primitive values — and offers no iteration or size property.
const ws = new WeakSet();
ws.add('string'); // TypeError
ws.size; // undefinedSame Restrictions as WeakMap
3WeakSet | JavaScript Tutorial - In-Depth Guide Part 3
The classic WeakSet use case is marking objects as 'already processed' without preventing them from being garbage collected once they're no longer needed elsewhere.
const visited = new WeakSet();
function processNode(node) {
if (visited.has(node)) return;
visited.add(node);
// ... expensive processing
}Marking Processed Objects
4WeakSet | JavaScript Tutorial - In-Depth Guide Part 4
WeakSet is also used to implement 'branding' — checking whether an object was constructed by a specific class or factory, without exposing a public flag property.
const validInstances = new WeakSet();
function createWidget() {
const widget = { /* ... */ };
validInstances.add(widget);
return widget;
}
function isValidWidget(obj) {
return validInstances.has(obj);
}Object Branding Pattern
5WeakSet | JavaScript Tutorial - In-Depth Guide Part 5
If you ever need to iterate the tracked objects or know how many there are, WeakSet is the wrong tool — reach for a regular Set instead, accepting the memory-retention trade-off.
// Need to iterate or count? Use a regular Set:
const trackedItems = new Set(); // supports size, for...ofWhen WeakSet Is the Wrong Choice
6Step-by-Step Breakdown
WeakSet is to Set what WeakMap is to Map: it stores only objects, holds them weakly, and allows those objects to be garbage collected when nothing else references them.
Like WeakMap, WeakSet only accepts objects — never primitive values — and offers no iteration or size property.
Checkpoint: Can WeakSet store primitive values like numbers or strings?
- →Yes, any value type is supported
- →No, only objects are allowed
The classic WeakSet use case is marking objects as 'already processed' without preventing them from being garbage collected once they're no longer needed elsewhere.
WeakSet is also used to implement 'branding' — checking whether an object was constructed by a specific class or factory, without exposing a public flag property.
If you ever need to iterate the tracked objects or know how many there are, WeakSet is the wrong tool — reach for a regular Set instead, accepting the memory-retention trade-off.
Checkpoint: If you need to know how many objects are currently tracked, is WeakSet the right choice?
- →Yes, WeakSet has a reliable size property
- →No, use a regular Set instead if counting/iteration is needed
Next, we'll explore 'Promise.all()'.
Level Up 🚀
Advanced cheat sheets, SEO tricks, and interview prep for this topic.
Browser Support
Fully supported.
Fully supported.
Fully supported.
Fully supported.
Accessibility (A11y)
1Use WeakSet to Avoid Reprocessing Elements During Accessibility Audits
A script that recursively audits ARIA attributes across a large, dynamic DOM tree can use a WeakSet to avoid reprocessing the same element twice during a single pass, without permanently retaining references to elements that get removed afterward.
SEO Implications
- 1
No Direct SEO Effect
WeakSet is a memory-management tool; SEO relevance is limited to preventing memory-related performance degradation in long-running client-side sessions.
Best Practices
Use WeakSet for "Has This Object Been Seen?" Tracking, Not Data Storage
If you need to associate a value with the object (not just a yes/no membership check), WeakMap is the correct tool instead — WeakSet only answers 'is this object in the set?'.
Choose Between WeakSet and Set Based on Whether You Need Iteration
If your tracking logic never needs to enumerate or count what has been tracked, WeakSet's automatic cleanup is a pure win; if it does, a regular Set is the only option, accepting the memory-retention trade-off.
Frequent Bugs
Using a regular Set to track "already processed" objects in a long-running process (like a server handling many requests), causing steadily growing memory usage as processed objects are never released.
Switch to WeakSet so tracked objects can be garbage collected once they are no longer referenced elsewhere in the program.
Trying to log or count all currently-tracked items in a WeakSet for debugging purposes, and finding no way to do so.
If enumeration is genuinely needed, maintain a separate regular Set or array alongside the WeakSet, or reconsider whether a regular Set better fits the actual requirements.
Real-World Examples
Preventing Infinite Loops in a Recursive Object Traversal
A deep-clone or serialization utility needed to detect and skip objects it had already visited during traversal, to safely handle circular references without leaking memory afterward.
function traverse(obj, visited = new WeakSet()) {
if (visited.has(obj)) return;
visited.add(obj);
Object.values(obj).forEach((v) => {
if (v && typeof v === 'object') traverse(v, visited);
});
}