WeakMap better than Private Symbols? (was: direct_proxies "problem")

Brendan Eich brendan at mozilla.com
Thu Jan 10 08:54:41 PST 2013


Allen rightly has pointed out several times on es-discuss that WeakMaps 
require GC special handling and are costly, compared to private symbols. 
This is true not only for GC time, but especially for get/set runtime. 
Properties named by Symbols, when implemented, should have the same 
polymorphic-inline-caching-or-better fast paths as string-key properties.

/be

Brandon Benvie wrote:
> At the very least, WeakMaps are potentially usable today (depending on 
> the target audience) while symbols are nowhere in sight. I’ve gotten a 
> lot of mileage out of this function, which as you said provides a 
> similar effective result as symbols (they differ in that the WeakMap 
> version isn’t inherited):
>     function createStorage(creator){
>       var store = new WeakMap;
>       creator || (creator = Object.create.bind(null, null, {}));
>
>       return function(obj){
>         var priv = store.get(obj);
>         if (!priv) store.set(obj, priv = creator(obj));
>         return o;
>       };
>     }
>     // usage
>     var _ = createStorage();
>     function Ctor(val){
>       _(this).private = val;
>     }
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss


More information about the es-discuss mailing list