Use cases for WeakMap

Sam Tobin-Hochstadt samth at ccs.neu.edu
Sun May 15 15:41:57 PDT 2011


On Sun, May 15, 2011 at 6:31 PM, Allen Wirfs-Brock
<allen at wirfs-brock.com> 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.

Of course, to do this using ES.next WeakMaps would be annoying,
because *only* objects can be used as keys, but that's not really
relevant to the strong/weak distinction.
-- 
sam th
samth at ccs.neu.edu


More information about the es-discuss mailing list