Use cases for WeakMap

Brendan Eich brendan at
Sun May 15 16:06:59 PDT 2011

On May 15, 2011, at 3:41 PM, Sam Tobin-Hochstadt wrote:

> On Sun, May 15, 2011 at 6:31 PM, Allen Wirfs-Brock
> <allen at> wrote:
>> On May 14, 2011, at 5:03 PM, Oliver Hunt wrote:
>>> 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.
>>> --Oliver
>> I think I've suggest in the past that what we are currently calling "WeakMap" should just be called "ObjectMap" or something like that.  I can't think of any use case where object identify is used as a map key and you don't want the "WeakMap" semantics.  Essentially "WeakMap" just means "NonLeakyObjectMap".
> It's pretty easy to implement a Set datastructure on top of a Map, and
> if your set's equality is ===, then you'd want a strong
> object-identity-keyed map.  WeakMap semantics would clearly be wrong
> here.

I talked to Sam because it wasn't totally clear to me what the loss of a WeakMap entry if all clients of that Set implementation lost their keys would break. He replied "enumeration".

That's a good point but it disqualifies WeakMap out of the gate. WeakMaps are not enumerable, again to avoid leaking GC non-determinism.

Therefore I don't think ObjectMap is a better name than WeakMap. But neither is NonEnumerableEphemeronBasedWeakMap ;-).

> Of course, to do this using WeakMaps would be annoying,
> because *only* objects can be used as keys, but that's not really
> relevant to the strong/weak distinction.

This is a good point too. Not sure we've considered a value -> value map carefully yet.


More information about the es-discuss mailing list