Use cases for WeakMap

Brendan Eich brendan at mozilla.com
Mon May 16 00:23:15 PDT 2011


On May 16, 2011, at 12:11 AM, Erik Corry wrote:

> 2011/5/15 Brendan Eich <brendan at mozilla.com>:
>> Besides attaching metadata, weak maps are important for remembering the
>> wrapper or membrane for a given (frozen or not, built-in or "host", not to
>> be mutated) object identity. Mark and Andreas knows too well, so I'm
>> preaching to es-discuss in the To: line. This is not a use-case for weak
>> references.
> 
> Is there an extra 'not' in this sentence?  "weak maps are important
> [...] this is not a use-case for weak references"

Weak references != weak maps.

http://wiki.ecmascript.org/doku.php?id=strawman:weak_references


> Mark has mentioned membranes as an example of the use of WeakMaps.  I
> can see that you don't want the membrane to keep the objects alive,
> but is it a problem that the objects keep the membrane alive?  Are we
> expecting lots of membranes to come and go and the GC will need to
> clean up after them?  I'm not saying it isn't important, I'm just
> trying to clarify the use case here.

I'm not sure what this has to do with weak references not being usable -- at all -- for membranes associated with (possibly frozen, not-to-be-mutated-in-any-event) objects.

There is no "there" in which to store a weak reference to the membrane from the object. Pigeon-hole problem, frozen object vs. mutation problem, host-object with crazy/zero storage semantics problem, the list goes on.

But in case it helps: yes, membranes need to be GC'ed ahead of their wrapped objects.

/be



More information about the es-discuss mailing list