Usage for weak-maps

Boris Zbarsky bzbarsky at MIT.EDU
Fri Oct 29 06:28:59 PDT 2010


On 10/29/10 4:50 AM, Peter van der Zee wrote:
> What's the use case for weak maps? What would you do with it that
> currently impossible and why is the workaround (if any) problematic
> enough to warrant a weak map implementation?

One case this came up recently was for Firebug,  Firebug wants to cache 
information about DOM Windows (the set of scripts compiled for that 
window, etc) and map from a Window to the information, but it obviously 
doesn't want to change the state of the Windows by adding properties, 
nor does it want to leak every single Window object.

There is no in-language workaround for this use case; we solved it in 
Gecko by adding special out-of-band API that assigns a numeric ID to 
each window and exposes that ID on an object that is not the window 
itself and which content doesn't have access to, so that Firebug can use 
the IDs as the keys in its map.

The use case generalizes, of course, to any situation where you want to 
store information for/about objects without sticking it on the object 
itself.

-Boris


More information about the es-discuss mailing list