Use cases for WeakMap

Erik Corry erik.corry at
Mon May 16 01:52:56 PDT 2011

2011/5/16 Brendan Eich <brendan at>:
> On May 16, 2011, at 12:11 AM, Erik Corry wrote:
>> 2011/5/15 Brendan Eich <brendan at>:
>>> 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.

Thanks, now I see what you meant.  Don't know how I missed that.

>> 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.

Nothing, that was just me reading 'weak references' as 'weak maps'.

> 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.

Perhaps I was unclear:  The semantics of Weak Maps are essentially
symmetric.  If you have the map and the key you can get the value.  If
you have just one then you can't (and the system may GC the value).
My question was, do the use cases have both the GC of the map and the
key triggering the GC of the value or is the GC of the key the
important one and GC of the map not that common/important etc.

For the use case mentioned by Boris in this thread, where a FF
extension needs to attach metadata to an object it doesn't seem likely
that the mapping will get lost and need to be GCed before the objects
that have the metadata attached.

> 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