FWIW the Multiple WeakMaps approach seems to be 2X ( Chrome ) up to 3X ( FF
Nightly ) faster than single WeakMap approach but I don't know how much
WeakMap instances cost in terms of GC operations so this bench might be
very pointless ( and surely it's premature )

Weirdly enough, Chrome Canary seems to be able to optimize the single
WeakMap approach pretty well, with a gap of 1.25X faster perfs on multiple
WMs.

the bench: http://jsperf.com/abstract-reference

the multiple WeakMaps approach:

```js
var PrivateCoordinatesMWM = (function () {

  var
    privates = {
      x: new WeakMap,
      y: new WeakMap
    }
  ;

  function __(self, key, value) {
    return arguments.length === 3 ?
      (privates[key].set(self, value), value) :
      privates[key].get(self);
  }

  function PrivateCoordinatesMWM(x, y) {
    __(this, 'x', x);
    __(this, 'y', y);
  }

  PrivateCoordinatesMWM.prototype.toString = function toString() {
    return ''.concat(
      '[', __(this, 'x'), 'x', __(this, 'y'), ']'
    );
  };

  return PrivateCoordinatesMWM;

}());
```

and the single WeakMap approach:

```js
var PrivateCoordinatesSWM = (function () {

  var privates = new WeakMap;

  function __(self, key, value) {
    var pvt = privates.get(self);
    if (arguments.length === 3) {
      if (!pvt) {
        privates.set(self, pvt = {});
      }
      return (pvt[key] = value);
    }
    return pvt && pvt[key];
  }

  function PrivateCoordinatesSWM(x, y) {
    __(this, 'x', x);
    __(this, 'y', y);
  }

  PrivateCoordinatesSWM.prototype.toString = function toString() {
    return ''.concat(
      '[', __(this, 'x'), 'x', __(this, 'y'), ']'
    );
  };

  return PrivateCoordinatesSWM;

}());
```

Best Regards


On Wed, Oct 22, 2014 at 7:59 AM, Andreas Rossberg <rossb...@google.com>
wrote:

> On 21 October 2014 22:31, Mark S. Miller <erig...@google.com> wrote:
> >> in saying that [weak maps] are not designed to work efficiently in that
> manner.
> >
> > Actually, they are. The problem is that initial implementations date from
> > before we appreciated the need for the transposed representation. The
> best
> > thing TC39 can do for the future of WeakMaps is to proceed assuming the
> > transposed representation.
>
> While I sympathise, let me clarify that this still remains a
> conjecture. AFAIK, nobody has proved it empirically in a JS
> implementation yet, we don't know in detail how complex such an
> implementation would be, and what side effects it might have on
> general performance (e.g., via added polymorphism). It's most likely
> not as easy or as clear a win as you may think it is.
>
> /Andreas
> _______________________________________________
> es-discuss mailing list
> es-discuss@mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss
>
_______________________________________________
es-discuss mailing list
es-discuss@mozilla.org
https://mail.mozilla.org/listinfo/es-discuss

Reply via email to