Use cases for WeakMap

Brendan Eich brendan at mozilla.com
Sun May 15 11:36:53 PDT 2011


On May 15, 2011, at 11:20 AM, Rick Waldron wrote:

> Thanks Brendan, I was looking for something that was representative of Boris's use-case, but I appreciate the snippet none-the-less.

Sorry, I should have realized what you were after (although in our gist discussion I thought I addressed it :-P).


> I should clarify my intentions; while your example is very straight forward, it's also as ambiguous as the gist I had originally posted that Alex, yourself and I shared a discussion on - only in that it doesn't illustrate the behaviour of the weakmap that has had a nulled key, post-GC.

As noted, you can't illustrate that difference without exposing the non-determistic GC schedule. We don't want to do that. With an #ifdef DEBUG, REPL/console-only hook, we could.


> I'm coming at this from the perspective of an js application developer and thinking  "how do I synthesize this into something that developers can understand the benefit of and apply to real-world uses?", in short: developer education/relations.

It's more of a "use this unless you really know what you are doing with strong references". Using Map by default is going to make leaks in the real world, guaranteed. Using WeakMap when in doubt will cost a bit more but avoid leaks.

/be


> 
> Either way, thanks again!
> 
> Rick
> 
> 
> On Sun, May 15, 2011 at 2:02 PM, Brendan Eich <brendan at mozilla.com> wrote:
> On May 14, 2011, at 6:06 PM, Rick Waldron wrote:
> 
>> Boris, 
>> 
>> Would you mind sharing a piece of working code (as in, runs in the latest Firefox Nightly) that demonstrates your example in a real world scenario? This would be greatly appreciated
> 
> javascript:k = {}; v = "hi"; w = WeakMap(); w.set(k, v); alert(w.get(k))
> 
> just alerted "hi" in my slightly out of date tracemonkey-repo nightly build. There may be some javascript: URL stripping or blocking you have to get past, in which case use the code after the javascript: in the Web Console.
> 
> /be
> 
> 
>> 
>> Rick
>> 
>> On Sat, May 14, 2011 at 8:55 PM, Boris Zbarsky <bzbarsky at mit.edu> wrote:
>> On 5/14/11 6:37 PM, Oliver Hunt wrote:
>> Can you provide a use case where you have an object key as the usual programming idiom?
>> 
>> Attaching metadata to object without polluting the objects themselves.
>> 
>> For example, Firefox extensions want to do this all the time for various DOM objects (and especially Window).
>> 
>> -Boris
>> 
>> _______________________________________________
>> es-discuss mailing list
>> es-discuss at mozilla.org
>> https://mail.mozilla.org/listinfo/es-discuss
>> 
>> _______________________________________________
>> es-discuss mailing list
>> es-discuss at mozilla.org
>> https://mail.mozilla.org/listinfo/es-discuss
> 
> 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20110515/0eaaf71d/attachment-0001.html>


More information about the es-discuss mailing list