Questioning WeakMap.prototype.clear

David Bruant bruant.d at gmail.com
Mon Jan 21 12:25:51 PST 2013


Le 21/01/2013 20:52, Allen Wirfs-Brock a écrit :
> On Jan 21, 2013, at 11:36 AM, Rick Waldron wrote:
>
>> This is the reality check I can get behind—I'm hard pressed to come up with a use case that isn't contrived or solvable by some other means.
>>
> This is easy:
>
> I'm do phased traversals over a complex data structure.  I have a number of functions that collaborative perform the function and they share access to a WeakMap to cache relationships that they identify over the course of the traversal.  When I start a new traversal phase I want to flush the cache so I use the clear method to do so.
Creating a new weakmap would work equally well to flush the cache.
Note that the current WeakMap.prototype.clear method is specified as:
"Set the value of M’s [[WeakMapData]] internal data property to a new 
empty List." which is quite close from creating a new weakmap.
If you care about reusing the same object, Mark's encapsulation [1]+[2] 
works too.

I intuit perf differences can be made marginal by engines in both cases. 
If I were wrong on that, implementors or authors will tell.
The Firefox precedent shows that it took ~3 weeks from the bug being 
filed to implementation to add .clear to an existing WeakMap 
implementation, so I'm not too worried about that either.

I think .clear can wait and people would need to clear can either create 
new weakmaps or wrap weakmaps as Mark showed.

David

[1] https://mail.mozilla.org/pipermail/es-discuss/2013-January/028370.html
[2] https://mail.mozilla.org/pipermail/es-discuss/2013-January/028371.html


More information about the es-discuss mailing list