privacy

JavaScript performance comparison

Test case created by

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  PublicPerson = function (name, age) {
      this.name = name;
      this.age = age;
  };
  
  PrivatePerson = (function () {
      var privates = new WeakMap();
  
      function Person(name, age) {
          privates.set(this, {
              name: name,
              age: age
          });
      }
  
      Person.prototype.name = function () {
          return privates.get(this).name;
      };
  
      Person.prototype.sameAge = function (other) {
          return privates.get(this).age === privates.get(other).age;
      };
  
      return Person;
  }());
  
  PrivyPerson = (function () {
      var key, value;
      
      var seal = function (object) {
          value = object;
          return key = {};
      };
  
      var open = function (proof) {
          var object = value;
          if (proof === key) {
               value = undefined;
               return object;
          }
      };
  
      var p = function (object) {
          return open(object._());
      };
  
      function Person(name, age) {
          var privates = { name: name, age: age };
  
          this._ = function () {
              return seal(privates);
          }
      }
  
      Person.prototype.name = function () {
          return p(this).name;
      };
  
      Person.prototype.sameAge = function (other) {
          return p(this).age === p(other).age;
      };
  
      return Person;
  }());
  
  PrivyPerson2 = (function () {
      var key, value;
      
      var seal = function (object) {
          value = object;
          return key = {};
      };
  
      var open = function (proof) {
          var object = value;
          if (proof === key) {
               value = undefined;
               return object;
          }
      };
  
      function Person(name, age) {
          var privates = { name: name, age: age };
  
          this._ = function () {
              return seal(privates);
          }
      }
  
      Person.prototype.name = function () {
          return open(this._()).name;
      };
  
      Person.prototype.sameAge = function (other) {
          return open(this._()).age === open(other._()).age;
      };
  
      return Person;
  }());
  
  publicPerson = new PublicPerson("thomas", 22);
  privatePerson = new PrivatePerson("thomas", 22);
  privyPerson = new PrivyPerson("thomas", 22);
  privyPerson2 = new PrivyPerson2("thomas", 22);

};
</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
Public
publicPerson.name === publicPerson.name;
publicPerson.age === publicPerson.age;
pending…
Private
privatePerson.name() === privatePerson.name();
privatePerson.sameAge(privatePerson);
pending…
Privy
privyPerson.name() === privyPerson.name();
privyPerson.sameAge(privyPerson);
pending…
Privy 2
privyPerson2.name() === privyPerson2.name();
privyPerson2.sameAge(privyPerson2);
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