WeakSet
A WeakSet is a collection of garbage-collectable values, including objects and non-registered symbols. A value in the WeakSet may only occur once. It is unique in the WeakSet's collection.
Browser support
| Feature | Desktop | Mobile | ||||
|---|---|---|---|---|---|---|
| Chrome | Edge | Firefox | Safari | Chrome Android | Safari iOS | |
| 36 | 12 | 34 | 9 | 36 | 9 | |
| The WeakSet() constructor creates WeakSet objects. | 36 | 12 | 34 | 9 | 36 | 9 |
WeakSet.WeakSet.iterable allowed `new WeakSet(iterable)` | 38 | 12 | 34 | 9 | 38 | 9 |
WeakSet.WeakSet.null allowed `new WeakSet(null)` | 36 | 12 | 37 | 9 | 36 | 9 |
| The add() method of WeakSet instances inserts the specified value into this set, if it is not already present. | 36 | 12 | 34 | 9 | 36 | 9 |
| The delete() method of WeakSet instances removes the specified value from this set, if it is in the set. | 36 | 12 | 34 | 9 | 36 | 9 |
| The has() method of WeakSet instances returns a boolean indicating whether the specified value exists in this WeakSet or not. | 36 | 12 | 34 | 9 | 36 | 9 |
WeakSet.symbol as keys Non-registered symbols as keys | 109 | 109 | 146 | 16.4 | 109 | 16.4 |
Syntax
const processed = new WeakSet();
function handle(obj) {
if (processed.has(obj)) return; // Already processed
processed.add(obj);
// Execute processing
} Live demo
Track processed objects
onedegreeprocessingobject WeakSet to, doubleprocessing prevention..
Simulate circular-reference detection
re processing to, in to object recordnoneloop preventsimul-tion..
Use cases
-
Using WeakSet
A WeakSet is a collection of garbage-collectable values, including objects and non-registered symbols.
Cautions
- No specific concerns. Stable across all major browsers.
Accessibility
- When updating the DOM dynamically, announce important changes to assistive technology with aria-live regions.