WeakSet

You’re reading the English version of this content since no translation exists yet for this locale. Help us translate this article!

The WeakSet object lets you store weakly held objects in a collection.

Syntax

new WeakSet([iterable]);

Parameters

iterable
If an iterable object is passed, all of its elements will be added to the new WeakSet. null is treated as undefined.

Examples

Using the WeakSet object

var ws = new WeakSet();
var foo = {};
var 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.

Description

WeakSet objects are collections of objects. An object in the WeakSet may occur only once; it is unique in the WeakSet's collection.

The main differences to the Set object are:

  • In contrast to Sets, WeakSets are collections of objects only and not of arbitrary values of any type.
  • The WeakSet is weak: References to objects in the collection are held weakly. If there is no other reference to an object stored in the WeakSet, they can be garbage collected. That also means that there is no list of current objects stored in the collection. WeakSets are not enumerable.

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:

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

Properties

WeakSet.length
The value of the length property is 0.
WeakSet.prototype
Represents the prototype for the WeakSet constructor. Allows the addition of properties to all WeakSet objects.

WeakSet instances

All WeakSet instances inherit from WeakSet.prototype.

Properties

WeakSet.prototype.constructor
Returns the function that created an instance's prototype. This is the WeakSet function by default.

Methods

WeakSet.prototype.add(value)
Appends a new object with the given value to the WeakSet object.
WeakSet.prototype.delete(value)
Removes the element associated to the value. WeakSet.prototype.has(value) will return false afterwards.
WeakSet.prototype.has(value)
Returns a boolean asserting whether an element is present with the given value in the WeakSet object or not.
WeakSet.prototype.clear()
Removes all elements from the WeakSet object.

Specifications

Specification Status Comment
ECMAScript 2015 (6th Edition, ECMA-262)
The definition of 'WeakSet' in that specification.
Standard Initial definition.
ECMAScript Latest Draft (ECMA-262)
The definition of 'WeakSet' in that specification.
Draft

Browser compatibility

Update compatibility data on GitHub
DesktopMobileServer
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidFirefox for AndroidOpera for AndroidSafari on iOSSamsung InternetNode.js
WeakSetChrome Full support 36Edge Full support 12Firefox Full support 34IE No support NoOpera Full support 23Safari Full support 9WebView Android Full support 37Chrome Android Full support 36Firefox Android Full support 34Opera Android Full support 24Safari iOS Full support 9Samsung Internet Android Full support 3.0nodejs Full support 0.12
addChrome Full support 36Edge Full support 12Firefox Full support 34IE No support NoOpera Full support 23Safari Full support 9WebView Android Full support 37Chrome Android Full support 36Firefox Android Full support 34Opera Android Full support 24Safari iOS Full support 9Samsung Internet Android Full support 3.0nodejs Full support 0.12
clear
DeprecatedNon-standard
Chrome No support 36 — 43Edge No support NoFirefox No support 34 — 46IE No support NoOpera No support 25 — 30Safari No support NoWebView Android No support 37 — 43Chrome Android No support 36 — 43Firefox Android No support 34 — 46Opera Android No support 25 — 30Safari iOS No support NoSamsung Internet Android No support 3.0 — 4.0nodejs No support No
deleteChrome Full support 36Edge Full support 12Firefox Full support 34IE No support NoOpera Full support 23Safari Full support 9WebView Android Full support 37Chrome Android Full support 36Firefox Android Full support 34Opera Android Full support 24Safari iOS Full support 9Samsung Internet Android Full support 3.0nodejs Full support 0.12
hasChrome Full support 36Edge Full support 12Firefox Full support 34IE No support NoOpera Full support 23Safari Full support 9WebView Android Full support 37Chrome Android Full support 36Firefox Android Full support 34Opera Android Full support 24Safari iOS Full support 9Samsung Internet Android Full support 3.0nodejs Full support 0.12
prototypeChrome Full support 36Edge Full support 12Firefox Full support 34IE No support NoOpera Full support 23Safari Full support 9WebView Android Full support 37Chrome Android Full support 36Firefox Android Full support 34Opera Android Full support 24Safari iOS Full support 9Samsung Internet Android Full support 3.0nodejs Full support 0.12
new WeakSet(iterable)Chrome Full support 38Edge Full support 12Firefox Full support 34IE No support NoOpera Full support 25Safari Full support 9WebView Android Full support 38Chrome Android Full support 38Firefox Android Full support 34Opera Android Full support 25Safari iOS Full support 9Samsung Internet Android Full support 3.0nodejs Full support 0.12
new WeakSet(null)Chrome Full support 36Edge Full support 12Firefox Full support 37IE No support NoOpera Full support 23Safari Full support 9WebView Android Full support 37Chrome Android Full support 36Firefox Android Full support 37Opera Android Full support 24Safari iOS Full support 9Samsung Internet Android Full support 3.0nodejs 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.

See also