It is my understanding of WeakMap
that "References to objects in the collection are held weakly. If there is no other reference to an object stored in the WeakMap, they can be garbage collected."
Why do the following key/value pairs still appear in the WeakMap after the references have been removed? Shouldn't the WeakMap be empty?
let dog1 = {name: 'Snickers'};
let dog2 = {name: 'Sunny'};
var strong = new Map();
var weak = new WeakMap();
strong.set(dog1, 'Snickers is the best!');
strong.set(dog2, 'Sunny is the 2nd best!');
weak.set(dog1, 'Snickers is the best!');
weak.set(dog2, 'Sunny is the 2nd best!');
dog1 = null;
dog2 = null;
console.log(strong);
console.log(weak);
/*
Output
Map(2) {{…} => "Snickers is the best!", {…} => "Sunny is the 2nd best!"}
WeakMap {{…} => "Snickers is the best!", {…} => "Sunny is the 2nd best!"}
*/
setTimeout(function(){
console.log("1200ms later... waiting for garbarge collection");
console.log(strong);
console.log(weak);
}, 1200);
/*
Output
Map(2) {{…} => "Snickers is the best!", {…} => "Sunny is the 2nd best!"}
WeakMap {{…} => "Snickers is the best!", {…} => "Sunny is the 2nd best!"}
*/