The WeakSet
object lets you store weakly held objects in a collection.
Description
WeakSet
objects are collections of objects. Just as with Set
s, each object in a WeakSet
may occur only once; all objects in a WeakSet
's collection are unique.
The main differences to the Set
object are:
WeakSets
are collections of objects only. They cannot contain arbitrary values of any type, asSet
s can.- The
WeakSet
is weak, meaning references to objects in aWeakSet
are held weakly. If no other references to an object stored in theWeakSet
exist, those objects can be garbage collected.-
Note: This also means that there is no list of current objects stored in the collection.
WeakSets
are not enumerable.
-
Use case: Detecting circular references
Functions that call themselves recursively need a way of guarding against circular data structures by tracking which objects have already been processed.
WeakSet
s are ideal for this purpose:
// Execute a callback on everything stored inside an object function execRecursively(fn, subject, _refs = null){ if(!_refs) _refs = new WeakSet(); // Avoid infinite recursion if(_refs.has(subject)) return; fn(subject); if("object" === typeof subject){ _refs.add(subject); for(let key in subject) execRecursively(fn, subject[key], _refs); } } const foo = { foo: "Foo", bar: { bar: "Bar" } }; foo.bar.baz = foo; // Circular reference! execRecursively(obj => console.log(obj), foo);
Here, a WeakSet
is created on the first run, and passed along with every subsequent function call (using the internal _refs
parameter).
The number of objects or their traversal order is immaterial, so a WeakSet
is more suitable (and performant) than a Set
for tracking object references, especially if a very large number of objects is involved.
Constructor
WeakSet()
- Creates a new
WeakSet
object.
Instance methods
WeakSet.prototype.add(value)
- Appends
value
to theWeakSet
object. WeakSet.prototype.delete(value)
- Removes
value
from theWeakSet
.WeakSet.prototype.has(value)
will returnfalse
afterwards. WeakSet.prototype.has(value)
- Returns a boolean asserting whether
value
is present in theWeakSet
object or not.
Examples
Using the WeakSet object
const ws = new WeakSet(); const foo = {}; const bar = {}; ws.add(foo); ws.add(bar); ws.has(foo); // true ws.has(bar); // true ws.delete(foo); // removes foo from the set ws.has(foo); // false, foo has been removed ws.has(bar); // true, bar is retained
Note that foo !== bar
. While they are similar objects, they are not the same object. And so they are both added to the set.
Specifications
Specification |
---|
ECMAScript (ECMA-262) The definition of 'WeakSet' in that specification. |
Browser compatibility
Desktop | Mobile | Server | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
WeakSet | Chrome Full support 36 | Edge Full support 12 | Firefox Full support 34 | IE No support No | Opera Full support 23 | Safari Full support 9 | WebView Android Full support 37 | Chrome Android Full support 36 | Firefox Android Full support 34 | Opera Android Full support 24 | Safari iOS Full support 9 | Samsung Internet Android Full support 3.0 | nodejs Full support 0.12 |
WeakSet() constructor | Chrome Full support 36 | Edge Full support 12 | Firefox Full support 34 | IE No support No | Opera Full support 23 | Safari Full support 9 | WebView Android Full support 37 | Chrome Android Full support 36 | Firefox Android Full support 34 | Opera Android Full support 24 | Safari iOS Full support 9 | Samsung Internet Android Full support 3.0 | nodejs Full support 0.12 |
add | Chrome Full support 36 | Edge Full support 12 | Firefox Full support 34 | IE No support No | Opera Full support 23 | Safari Full support 9 | WebView Android Full support 37 | Chrome Android Full support 36 | Firefox Android Full support 34 | Opera Android Full support 24 | Safari iOS Full support 9 | Samsung Internet Android Full support 3.0 | nodejs Full support 0.12 |
clear | Chrome No support 36 — 43 | Edge No support No | Firefox No support 34 — 46 | IE No support No | Opera No support 25 — 30 | Safari No support No | WebView Android No support 37 — 43 | Chrome Android No support 36 — 43 | Firefox Android No support 34 — 46 | Opera Android No support 25 — 30 | Safari iOS No support No | Samsung Internet Android No support 3.0 — 4.0 | nodejs No support No |
delete | Chrome Full support 36 | Edge Full support 12 | Firefox Full support 34 | IE No support No | Opera Full support 23 | Safari Full support 9 | WebView Android Full support 37 | Chrome Android Full support 36 | Firefox Android Full support 34 | Opera Android Full support 24 | Safari iOS Full support 9 | Samsung Internet Android Full support 3.0 | nodejs Full support 0.12 |
has | Chrome Full support 36 | Edge Full support 12 | Firefox Full support 34 | IE No support No | Opera Full support 23 | Safari Full support 9 | WebView Android Full support 37 | Chrome Android Full support 36 | Firefox Android Full support 34 | Opera Android Full support 24 | Safari iOS Full support 9 | Samsung Internet Android Full support 3.0 | nodejs Full support 0.12 |
Legend
- Full support
- Full support
- No support
- No support
- Non-standard. Expect poor cross-browser support.
- Non-standard. Expect poor cross-browser support.
- Deprecated. Not for use in new websites.
- Deprecated. Not for use in new websites.