Use cases for WeakMap

David Bruant david.bruant at labri.fr
Sat May 14 17:11:31 PDT 2011


Le 15/05/2011 02:03, Oliver Hunt a écrit :
> I suspect i did suggest WeakMap but I think I misunderstood the
> proposal.  I felt the goal was to prevent the key from being kept
> around forever even when the value was gone, I did not expect the key
> to keep the value alive.
I may be missing something, but I think there is no way to do differently.
----
var wm = WeakMap();
var value = {};
var key = {};

wm.set(key, value);
value = null; // value can be garbage-collected anytime now.

wm.get(key); // ?
----
Let's suppose "key" can be garbage-collected if "value" is
garbage-collected as you suggest.
What is the value returned by the get?
"value" if it hasn't been garbage-collected or "undefined" if it has?
This is a garbage-collector leak and introduces indeterminism.

David


More information about the es-discuss mailing list