Argument in favor of adding "has" in the WeakMap API

Brendan Eich brendan at
Wed May 11 13:45:41 PDT 2011

On May 11, 2011, at 1:04 PM, Oliver Hunt wrote:

> On May 11, 2011, at 12:54 PM, Brendan Eich wrote:
>> On May 11, 2011, at 12:44 PM, Oliver Hunt wrote:
>>> So you want to do
>>> if (map.has(bar))
>>> wiffle = map.get(bar)
>>> or some such?
>>> The problem here is that you can't guarantee that GC won't happen between those two calls, and therefore you could still end up getting undefined in response to the get.
>> Not if bar refers to the key object, since it must be a strong ref.
> Re-reading the weakmap definition makes weakmaps seem fundamentally different to any other weakmap API i've ever encountered.  The most common weakmap use cases i am aware of are all essentially caches, and this definition doesn't support that use case at all.

WeakMap is useful for caching. You just have to kill your strong refs if you don't want to hit the cache.

> It seems completely bizarre to me that the key should act as a root for the value it is associated with.

You must be assuming key is a "weak pointer" that magically gets nulled when the GC runs and finds only the variable named key and the map entry referencing (weakly) the key object. But there is no such notion, because we do not want to leak the non-determistic GC schedule by allowing users to poll for null.


More information about the es-discuss mailing list