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.

* Some parts of this feature may have varying levels of support.

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.

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:

  • WeakSets are collections of objects and symbols only. They cannot contain arbitrary values of any type, as Sets can.

  • The WeakSet is weak, meaning references to objects in a WeakSet are held weakly. If no other references to a value stored in the WeakSet 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.

Key equality

Like regular Set, value equality is based on the SameValueZero algorithm, which is the same as the === operator because WeakSet can only hold object and symbol values. This means that for object values, equality is based on object identity. They are compared by reference, not by value.

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 the WeakSet constructor.

WeakSet.prototype[Symbol.toStringTag]

The initial value of the [Symbol.toStringTag] property is the string "WeakSet". This property is used in Object.prototype.toString().

Instance methods

WeakSet.prototype.add()

Inserts the specified value into this set, if it is not already present.

WeakSet.prototype.delete()

Removes the specified value from this set, if it is in the set.

WeakSet.prototype.has()

Returns a boolean indicating whether the specified value exists in this WeakSet or not.

Examples

Using the WeakSet object

js
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.

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.

WeakSets are ideal for this purpose:

js
// 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.

Specifications

Specification
ECMAScript® 2027 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
WebView on iOS
Bun
Deno
Node.js
WeakSet
Chrome – Full support
Chrome 36 (Release date: 2014-07-16)
footnote Full support
Edge – Full support
Edge 12 (Release date: 2015-07-29)
footnote Full support
Firefox – Full support
Firefox 34 (Release date: 2014-12-01)
footnote Full support
Opera – Full support
Opera 23 (Release date: 2014-07-22)
footnote Full support
Safari – Full support
Safari 9 (Release date: 2015-09-30)
footnote Full support
Chrome Android – Full support
Chrome Android 36 (Release date: 2014-07-16)
footnote Full support
Firefox for Android – Full support
Firefox for Android 34 (Release date: 2014-12-01)
footnote Full support
Opera Android – Full support
Opera Android 24 (Release date: 2014-09-10)
footnote Full support
Safari on iOS – Full support
Safari on iOS 9 (Release date: 2015-09-16)
footnote Full support
Samsung Internet – Full support
Samsung Internet 3 (Release date: 2015-04-10)
footnote Full support
WebView Android – Full support
WebView Android 37 (Release date: 2014-09-03)
footnote Full support
WebView on iOS – Full support
WebView on iOS 9 (Release date: 2015-09-16)
footnote Full support
Bun – Full support
Bun 1 (Release date: 2023-09-08)
footnote Full support
Deno – Full support
Deno 1 (Release date: 2020-05-13)
footnote Full support
Node.js – Full support
Node.js 0.12 (Release date: 2015-02-06)
footnote Full support
WeakSet() constructor
Chrome – Full support
Chrome 36 (Release date: 2014-07-16)
footnote Full support
Edge – Full support
Edge 12 (Release date: 2015-07-29)
footnote Full support
Firefox – Full support
Firefox 34 (Release date: 2014-12-01)
footnote Full support
Opera – Full support
Opera 23 (Release date: 2014-07-22)
footnote Full support
Safari – Full support
Safari 9 (Release date: 2015-09-30)
footnote Full support
Chrome Android – Full support
Chrome Android 36 (Release date: 2014-07-16)
footnote Full support
Firefox for Android – Full support
Firefox for Android 34 (Release date: 2014-12-01)
footnote Full support
Opera Android – Full support
Opera Android 24 (Release date: 2014-09-10)
footnote Full support
Safari on iOS – Full support
Safari on iOS 9 (Release date: 2015-09-16)
footnote Full support
Samsung Internet – Full support
Samsung Internet 3 (Release date: 2015-04-10)
footnote Full support
WebView Android – Full support
WebView Android 37 (Release date: 2014-09-03)
footnote Full support
WebView on iOS – Full support
WebView on iOS 9 (Release date: 2015-09-16)
footnote Full support
Bun – Full support
Bun 1 (Release date: 2023-09-08)
footnote Full support
Deno – Full support
Deno 1 (Release date: 2020-05-13)
footnote Full support
Node.js – Full support
Node.js 0.12 (Release date: 2015-02-06)
footnote Full support
new WeakSet(iterable)
Chrome – Full support
Chrome 38 (Release date: 2014-10-07)
footnote Full support
Edge – Full support
Edge 12 (Release date: 2015-07-29)
footnote Full support
Firefox – Full support
Firefox 34 (Release date: 2014-12-01)
footnote Full support
Opera – Full support
Opera 25 (Release date: 2014-10-15)
footnote Full support
Safari – Full support
Safari 9 (Release date: 2015-09-30)
footnote Full support
Chrome Android – Full support
Chrome Android 38 (Release date: 2014-10-08)
footnote Full support
Firefox for Android – Full support
Firefox for Android 34 (Release date: 2014-12-01)
footnote Full support
Opera Android – Full support
Opera Android 25 (Release date: 2014-10-16)
footnote Full support
Safari on iOS – Full support
Safari on iOS 9 (Release date: 2015-09-16)
footnote Full support
Samsung Internet – Full support
Samsung Internet 3 (Release date: 2015-04-10)
footnote Full support
WebView Android – Full support
WebView Android 38 (Release date: 2014-10-08)
footnote Full support
WebView on iOS – Full support
WebView on iOS 9 (Release date: 2015-09-16)
footnote Full support
Bun – Full support
Bun 1 (Release date: 2023-09-08)
footnote Full support
Deno – Full support
Deno 1 (Release date: 2020-05-13)
footnote Full support
Node.js – Full support
Node.js 0.12 (Release date: 2015-02-06)
footnote Full support
new WeakSet(null)
Chrome – Full support
Chrome 36 (Release date: 2014-07-16)
footnote Full support
Edge – Full support
Edge 12 (Release date: 2015-07-29)
footnote Full support
Firefox – Full support
Firefox 37 (Release date: 2015-03-31)
footnote Full support
Opera – Full support
Opera 23 (Release date: 2014-07-22)
footnote Full support
Safari – Full support
Safari 9 (Release date: 2015-09-30)
footnote Full support
Chrome Android – Full support
Chrome Android 36 (Release date: 2014-07-16)
footnote Full support
Firefox for Android – Full support
Firefox for Android 37 (Release date: 2015-03-31)
footnote Full support
Opera Android – Full support
Opera Android 24 (Release date: 2014-09-10)
footnote Full support
Safari on iOS – Full support
Safari on iOS 9 (Release date: 2015-09-16)
footnote Full support
Samsung Internet – Full support
Samsung Internet 3 (Release date: 2015-04-10)
footnote Full support
WebView Android – Full support
WebView Android 37 (Release date: 2014-09-03)
footnote Full support
WebView on iOS – Full support
WebView on iOS 9 (Release date: 2015-09-16)
footnote Full support
Bun – Full support
Bun 1 (Release date: 2023-09-08)
footnote Full support
Deno – Full support
Deno 1 (Release date: 2020-05-13)
footnote Full support
Node.js – Full support
Node.js 0.12 (Release date: 2015-02-06)
footnote Full support
add
Chrome – Full support
Chrome 36 (Release date: 2014-07-16)
footnote Full support
Edge – Full support
Edge 12 (Release date: 2015-07-29)
footnote Full support
Firefox – Full support
Firefox 34 (Release date: 2014-12-01)
footnote Full support
Opera – Full support
Opera 23 (Release date: 2014-07-22)
footnote Full support
Safari – Full support
Safari 9 (Release date: 2015-09-30)
footnote Full support
Chrome Android – Full support
Chrome Android 36 (Release date: 2014-07-16)
footnote Full support
Firefox for Android – Full support
Firefox for Android 34 (Release date: 2014-12-01)
footnote Full support
Opera Android – Full support
Opera Android 24 (Release date: 2014-09-10)
footnote Full support
Safari on iOS – Full support
Safari on iOS 9 (Release date: 2015-09-16)
footnote Full support
Samsung Internet – Full support
Samsung Internet 3 (Release date: 2015-04-10)
footnote Full support
WebView Android – Full support
WebView Android 37 (Release date: 2014-09-03)
footnote Full support
WebView on iOS – Full support
WebView on iOS 9 (Release date: 2015-09-16)
footnote Full support
Bun – Full support
Bun 1 (Release date: 2023-09-08)
footnote Full support
Deno – Full support
Deno 1 (Release date: 2020-05-13)
footnote Full support
Node.js – Full support
Node.js 0.12 (Release date: 2015-02-06)
footnote Full support
delete
Chrome – Full support
Chrome 36 (Release date: 2014-07-16)
footnote Full support
Edge – Full support
Edge 12 (Release date: 2015-07-29)
footnote Full support
Firefox – Full support
Firefox 34 (Release date: 2014-12-01)
footnote Full support
Opera – Full support
Opera 23 (Release date: 2014-07-22)
footnote Full support
Safari – Full support
Safari 9 (Release date: 2015-09-30)
footnote Full support
Chrome Android – Full support
Chrome Android 36 (Release date: 2014-07-16)
footnote Full support
Firefox for Android – Full support
Firefox for Android 34 (Release date: 2014-12-01)
footnote Full support
Opera Android – Full support
Opera Android 24 (Release date: 2014-09-10)
footnote Full support
Safari on iOS – Full support
Safari on iOS 9 (Release date: 2015-09-16)
footnote Full support
Samsung Internet – Full support
Samsung Internet 3 (Release date: 2015-04-10)
footnote Full support
WebView Android – Full support
WebView Android 37 (Release date: 2014-09-03)
footnote Full support
WebView on iOS – Full support
WebView on iOS 9 (Release date: 2015-09-16)
footnote Full support
Bun – Full support
Bun 1 (Release date: 2023-09-08)
footnote Full support
Deno – Full support
Deno 1 (Release date: 2020-05-13)
footnote Full support
Node.js – Full support
Node.js 0.12 (Release date: 2015-02-06)
footnote Full support
has
Chrome – Full support
Chrome 36 (Release date: 2014-07-16)
footnote Full support
Edge – Full support
Edge 12 (Release date: 2015-07-29)
footnote Full support
Firefox – Full support
Firefox 34 (Release date: 2014-12-01)
footnote Full support
Opera – Full support
Opera 23 (Release date: 2014-07-22)
footnote Full support
Safari – Full support
Safari 9 (Release date: 2015-09-30)
footnote Full support
Chrome Android – Full support
Chrome Android 36 (Release date: 2014-07-16)
footnote Full support
Firefox for Android – Full support
Firefox for Android 34 (Release date: 2014-12-01)
footnote Full support
Opera Android – Full support
Opera Android 24 (Release date: 2014-09-10)
footnote Full support
Safari on iOS – Full support
Safari on iOS 9 (Release date: 2015-09-16)
footnote Full support
Samsung Internet – Full support
Samsung Internet 3 (Release date: 2015-04-10)
footnote Full support
WebView Android – Full support
WebView Android 37 (Release date: 2014-09-03)
footnote Full support
WebView on iOS – Full support
WebView on iOS 9 (Release date: 2015-09-16)
footnote Full support
Bun – Full support
Bun 1 (Release date: 2023-09-08)
footnote Full support
Deno – Full support
Deno 1 (Release date: 2020-05-13)
footnote Full support
Node.js – Full support
Node.js 0.12 (Release date: 2015-02-06)
footnote Full support
Non-registered symbols as keys
Chrome – Full support
Chrome 109 (Release date: 2023-01-10)
footnote Full support
Edge – Full support
Edge 109 (Release date: 2023-01-12)
footnote Full support
Firefox – Full support
Firefox 146 (Release date: 2025-12-09)
footnote Full support
Opera – Full support
Opera 95 (Release date: 2023-02-01)
footnote Full support
Safari – Full support
Safari 16.4 (Release date: 2023-03-27)
footnote Full support
Chrome Android – Full support
Chrome Android 109 (Release date: 2023-01-10)
footnote Full support
Firefox for Android – Full support
Firefox for Android 146 (Release date: 2025-12-09)
footnote Full support
Opera Android – Full support
Opera Android 74 (Release date: 2023-03-13)
footnote Full support
Safari on iOS – Full support
Safari on iOS 16.4 (Release date: 2023-03-27)
footnote Full support
Samsung Internet – Full support
Samsung Internet 21 (Release date: 2023-05-19)
footnote Full support
WebView Android – Full support
WebView Android 109 (Release date: 2023-01-10)
footnote Full support
WebView on iOS – Full support
WebView on iOS 16.4 (Release date: 2023-03-27)
footnote Full support
Bun – Full support
Bun 1 (Release date: 2023-09-08)
footnote Full support
Deno – Full support
Deno 1.28 (Release date: 2022-11-13)
footnote Full support
Node.js – Full support
Node.js 20 (Release date: 2023-04-18)
footnote Full support

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support

See also