In MDN article on WeakSet
s, an example is given for using weak sets to avoid infinite recursion on circular references:
// Execute a callback on everything stored inside an object
function execRecursively(fn, subject, _refs = new WeakSet()) {
// Avoid infinite recursion
if (_refs.has(subject)) {
return;
}
// ...
If WeakSet
is intended to allow the objects to be garbage collected, doesn’t this mean that with sufficiently large objects and little memory a previously encountered object will be garbage collected and thus set’s .has()
will not return true, making infinite recursion possible?