Mark S. Miller erights at google.com
Wed Sep 14 18:20:18 PDT 2011

On Wed, Sep 14, 2011 at 6:04 PM, Juan Ignacio Dopazo
<dopazo.juan at gmail.com>wrote:

> On Wednesday, September 14, 2011, David Bruant <david.bruant at labri.fr>
> wrote:
> > Also, I would like to talk a little bit about terminology. WeakMaps have
> > their name inspired by the idea of "weak" references which have
> > particular garbage-collection properties. From the developer
> > perspective, this seems to be some sort of implementation detail they
> > should not be aware of.
> > As far as I know, current functions/constructors have their name
> > inspired by the contract they fulfill rather than implementation
> > considerations. The difference between current WeakMaps and Maps is
> > their contract. In the latter, keys can be enumerated, in the former
> > not. I think that this is the difference that should inspire different
> > names rather than the implementation optimisation that is induced by
> > this contract difference.
> >
> In the last few days I had to write a piece of code that would strongly
> benefit from WeakMaps. I needed to store information about DOM nodes and
> retrieve it later, and these nodes aren't in my control so they can be
> detached at any time by someone else. If the references I kept were weak,
> I'd be sure that I wouldn't be causing a memory leak. And that's important
> in this case because the nodes are very likely Flash objects which can
> easily mean 20-50mb in memory. So knowing that a reference is weak is
> important information.

I agree.

Normally I strongly take the same position David does: emphasize semantics
over implementation. But why? It is good when we can label a tool according
to its purpose, rather than how it accomplishes that purpose. Associating
the tool with its purpose helps us remember the right tool for the right
job. Few would reach for the WeakMap tool thinking "I need a non-enumerable
table". Granted, there are cases when the non-enumerability is the desired
feature, but those cases are rare. The common purpose of a WeakMap is rooted
in our understanding, at a high level, of certain implementation costs, and
our desire to avoid certain avoidable implementation costs. Generally, that
is what a WeakMap is *for*.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20110914/8d02c701/attachment.html>

More information about the es-discuss mailing list