This is covered in §23.3 of the specification:
If an object that is being used as the key of a WeakMap
key/value pair is only reachable by following a chain of references that start within that WeakMap
, then that key/value pair is inaccessible and is automatically removed from the WeakMap
.
So the entries in a weak map, if their keys aren't referenced by anything else, will be reclaimed by garbage collection at some point.
In contrast, a Map
holds a strong reference to its keys, preventing them from being garbage-collected if the map is the only thing referencing them.
MDN puts it like this:
The key in a WeakMap
is held weakly. What this means is that, if there are no other strong references to the key, then the entire entry will be removed from the WeakMap
by the garbage collector.
And WeakSet
does the same.
...in which situation should I favor the use of this data structures?
Any situation where you don't want the fact you have a map/set using a key to prevent that key from being garbage-collected. Here are some examples:
Having instance-specific information which is truly private to the instance, which looks like this: (Note: This example is from 2015, well before private fields were an option. Here in 2021, I'd use private fields for this.)
let Thing = (() => {
var privateData = new WeakMap();
class Thing {
constructor() {
privateData[this] = {
foo: "some value"
};
}
doSomething() {
console.log(privateData[this].foo);
}
}
return Thing;
})();
There's no way for code outside that scoping function to access the data in privateData
. That data is keyed by the instance itself. You wouldn't do that without a WeakMap
because it would be a memory leak, your Thing
instances would never be cleaned up. But WeakMap
only holds weak references, and so if your code using a Thing
instance is done with it and releases its reference to the instance, the WeakMap
doesn't prevent the instance from being garbage-collected; instead, the entry keyed by the instance is removed from the map.
Holding information for objects you don't control. Suppose you get an object from some API and you need to remember some additional information about that object. You could add properties to the object itself (if it's not sealed), but adding properties to objets outside of your control is just asking for trouble. Instead, you can use a WeakMap
keyed by the object to store your extra information.
One use case for WeakSet
is tracking or branding: Suppose that before "using" an object, you need to know whether that object has ever been "used" in the past, but without storing that as a flag on the object (perhaps because if it's a flag on the object, other code can see it [though you could use a private field to prevent that]; or because it's not your object [so private fields wouldn't help]). For instance, this might be some kind of single-use access token. A WeakSet
is a simple way to do that without forcing the object to stay in memory.
Which of the above data structures will produce the same/similar result of doing: let hash = Object.create(null); hash[index] = something;
That would be nearest to Map
, because the string index
(the property name) will be held by a strong reference in the object (it and its associated property will not be reclaimed if nothing else references it).
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…