Proposal: Abstract References

Andrea Giammarchi andrea.giammarchi at gmail.com
Wed Oct 22 05:18:13 PDT 2014


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 <rossberg at google.com>
wrote:

> On 21 October 2014 22:31, Mark S. Miller <erights at 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 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/20141022/efa8db2b/attachment.html>


More information about the es-discuss mailing list