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.
WeakSet
Baseline Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since September 2015.
Description
Values of WeakSets must be garbage-collectable. Most primitive data types can be arbitrarily created and don't have a lifetime, so they cannot be stored. Objects and non-registered symbols can be stored because they are garbage-collectable.
The main differences to the Set
object are:
-
WeakSet
s are collections of objects and symbols 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 a value stored in theWeakSet
exist, those values can be garbage collected.Note: This also means that there is no list of current values 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 = new WeakSet()) { // Avoid infinite recursion if (_refs.has(subject)) { return; } fn(subject); if (typeof subject === "object" && subject) { _refs.add(subject); for (const key in subject) { execRecursively(fn, subject[key], _refs); } _refs.delete(subject); } } 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 properties
These properties are defined on WeakSet.prototype
and shared by all WeakSet
instances.
WeakSet.prototype.constructor
-
The constructor function that created the instance object. For
WeakSet
instances, the initial value is theWeakSet
constructor. WeakSet.prototype[Symbol.toStringTag]
-
The initial value of the
[Symbol.toStringTag]
property is the string"WeakSet"
. This property is used inObject.prototype.toString()
.
Instance methods
WeakSet.prototype.add()
-
Appends
value
to theWeakSet
object. WeakSet.prototype.delete()
-
Removes
value
from theWeakSet
.WeakSet.prototype.has(value)
will returnfalse
afterwards. WeakSet.prototype.has()
-
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 Language Specification # sec-weakset-objects |
Browser compatibility
Desktop | Mobile | Server | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Chrome | Edge | Firefox | Opera | Safari | Chrome Android | Firefox for Android | Opera Android | Safari on IOS | Samsung Internet | WebView Android | Deno | Node.js | |
WeakSet |
36 | 12 | 34 | 23 | 9 | 36 | 34 | 24 | 9 | 3.0 | 37 | 1.0 | 0.12.0 |
WeakSet |
36 | 12 | 34 | 23 | 9 | 36 | 34 | 24 | 9 | 3.0 | 37 | 1.0 | 0.12.0 |
add |
36 | 12 | 34 | 23 | 9 | 36 | 34 | 24 | 9 | 3.0 | 37 | 1.0 | 0.12.0 |
delete |
36 | 12 | 34 | 23 | 9 | 36 | 34 | 24 | 9 | 3.0 | 37 | 1.0 | 0.12.0 |
has |
36 | 12 | 34 | 23 | 9 | 36 | 34 | 24 | 9 | 3.0 | 37 | 1.0 | 0.12.0 |
symbol_as_keys |
109 | 109 | No | 95 | 16.4 | 109 | No | 74 | 16.4 | 21.0 | 109 | 1.28 | 20.0.0 |
See also
© 2005–2024 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakSet