Home > Enterprise >  Why in WeakMap key and value are not removed when you display the entire collection, but are removed
Why in WeakMap key and value are not removed when you display the entire collection, but are removed

Time:06-18

Example with "nulling" value. Why does the element still exist in the WeakMap collection in this case?

let weakMap = new WeakMap();
let obj = {name: 'Ivan'};
//adding some new element
weakMap.set({}, obj);
obj = null;
console.log(weakMap);
console.log(weakMap.get({}));

Whole collection

enter image description here

One element

enter image description here

Example with "nulling" key:

enter image description here

What's going on here? What does it come from? The garbage collector does not have time to clean up everything or what?

CodePudding user response:

First of all weakMap.set({}, obj); and weakMap.get({}); will never refer to the same element in the WeakMap because they are separate, anonymous object literals. In order for the WeakMap to be useful you need to store a reference to the object to be used as key1.

let weakMap = new WeakMap();
let obj = {name: 'Ivan'};

weakMap.set({}, obj);
console.log('has anonymous key:', weakMap.has({}));
console.log(weakMap.get({}));

let key = {};
weakMap.set(key, obj);
console.log('has assigned key:', weakMap.has(key));
console.log(weakMap.get(key));

Secondly, it is the key that is weakly referenced in the WeakMap, not the value, so setting the value to null will have no effect on the WeakMap. Instead you need to 'null' the key2 (another reason you need a stored reference to it).

let weakMap = new WeakMap();
let obj = { name: 'Ivan' };

let key = {};
weakMap.set(key, obj);

console.log(weakMap);

key = null;

// no good way to indicate this, but debugging in the console will reveal an empty WeakMap pending garbage collection
console.log(weakMap);


1 Your example using an unreachable object as key will disappear from the WeakMap on garbage collection, but not because you set obj=null but because the only reference to the object exists in the WeakMap.

2 There is no good way to demonstrate the behaviour of the second snippet because, per the documentation, '... a WeakMap doesn't allow observing the liveness of its keys, its keys are not enumerable.' Nonetheless, debugging in the console will reveal the behaviour, though awkwardly given the variety of ways different browsers treat objects in the console, ie. chrome's live objects.

  • Related