Use cases for WeakMap

Brendan Eich brendan at mozilla.com
Mon May 16 10:48:19 PDT 2011


On May 16, 2011, at 1:52 AM, Erik Corry wrote:

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

I'm not sure, we will have to measure to give a quantitative answer. But consider this: weak maps are used with membranes and "vats", and both the wrapped object (a key into a weak map handling out-of-vat object wrapping) and the whole vat can go away. Think window.close or DOM iframe creation/destruction via navigation.


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

The add-on easily can outlive the content window or iframe containing the wrapped object, you're right. But it can go the other way too: add-on disabling or add-on specific reset clears the add-on state, including weak maps, while the content DOM wrapped objects live blithely on.

/be


More information about the es-discuss mailing list