I'm not really sure the above is showing you what a WeakSet is doing..
So I've created a snippet here to show it. Because from the browser you generally don't have access to the GC, I have made it so that it console logs the WeakSet, and waits for you to press a button and console logs again. In the mean time you can then clear the browsers console and force a GC, clearing the console is done as in Chrome console logging the WeakSet will also end up keeping a reference to the objects.
If you do this say in Chrome browser, you should see WeakSet {{..}, {{..}}
, showing us that the WeakSet has a reference to 2 objects. After clearing the console, forcing a GC, and clicking the button,. another WeakSet will be console logged, it should show WeakSet {{..}}
. basically showing 1 object, proving the GC has done it's job and is keeping a reference to objectA
only.
Note: In chrome to force a GC, go to the performance tab, and there is
an icon that looks like a dustbin 🗑️, click this.
ps. If you change the new WeakSet
to new Set
in the snippet, and do the same thing, you will notice you will still have 2 items in the Set. This is the difference between a Set and a WeakSet.
const ws = new WeakSet();
let objectA = {};
let objectB = {};
ws.add(objectA);
ws.add(objectB);
console.log(ws);
objectB = null;
document.querySelector("button").onclick = function () {
console.log(ws);
}
<p>Look inside Chrome console, you should see a WeakSet, and it should have two values in there. eg. <b>WeakSet {{..}},{..}}</b></p>
<p>Now clear the console, otherwise the console will keep a referece to the weakset entries, and either wait for a while, maybe 30 seconds, or go into chrome's Performace tab and click the collect garbage icon. After doing this, click the button below.</p>
<p>
<button>Click me after console clear and GC</button>
<p>After clicking the above button look in your console again, you should see a Weakset again, but with just one item. eg. <b>WeakSet {{..}}</b></p>