I was learning about use-cases of weakMaps, weakSets and weakRefs.
I came across a code which was written like this:
{
const x = {
a: [1, 2]
};
var weakMap = new WeakMap();
weakMap.set(x, 'something');
}
console.log(weakMap);
Note: Please see the console, after running the snippet.
Most of the times, when I run this code, I am getting the key x inside weakMap.
But for couple of times, I got noting inside weakMap, when I used the same code. What is the reason behind it?
I was watching this video for learning.
WeakMap with data in it for above JS code(Codepen).
WeakMap without data in it for above JS code(JSFiddle, I could reproduce this only once inside JSFiddle).
Is garbage collection unpredictable? I would like to know if you have ever used weakSet, weakMap or WeakRef in real-life coding. In what situation did you use it?