abstract-reference

JavaScript performance comparison

Test case created by WebReflection

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  var result = [];
  
  // Multiple WeakMaps approach
  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;
  
  }());
  
  // Single WeakMap approach
  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;
  
  }());

};
</script>

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
Multiple WeakMaps
result.push(
  new PrivateCoordinatesMWM(
    Math.random(),
    Math.random()
  ).toString()
);
pending…
Single WeakMap
result.push(
  new PrivateCoordinatesSWM(
    Math.random(),
    Math.random()
  ).toString()
);
pending…

Compare results of other browsers

Revisions

You can edit these tests or add even more tests to this page by appending /edit to the URL.

0 Comments