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

David Bruant david.bruant at
Wed May 11 13:24:39 PDT 2011

Le 11/05/2011 21:54, Brendan Eich a écrit :
> 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.
> If you do
>   if (map.has(bar)) {
>     bar = null;
>     // might GC here, or do stuff that might
>     wiffle = map.get(bar);
>     . . .
>   }
> then you get what you deserve.
>> GC is (from the language's perspective) non-deterministic, so any non-atomic operation with a weak map leads to "non-sensical" behaviour.
> True, but this does not apply to
>   if (map.has(bar))
>     wiffle = map.get(bar);
> as written.
Exactly. Said in another way, if you are able to call map.has(bar),
then, you hold a reference to bar, so the garbage collector cannot
consider collecting the object which bar references to (by definition of
"holding a reference").

As a side note, I didn't receive initial Olivier Hunt's e-mail. It
appears on es-discuss archive, but I didn't receive it. I am under the
impression that it's not the first time this happens to me. Am I the
only one in that case?


More information about the es-discuss mailing list