Closure vs. property (v4)

Revision 4 of this benchmark created by Axel Rauschmayer on


Setup

function ClosureColor(name) {
      // Have no choice but to add these methods to the instance
      this.getName = function () {
          return name;
      };
      this.setName = function (n) {
          name = n;
      };
  }
  var closureColor = new ClosureColor('green');
  
  
  function PropertyColor(name) {
      this._name = name;
      this.getName = function () {
          return this._name;
      };
      this.setName = function (n) {
          this._name = n;
      };
  }
  var propertyColor = new PropertyColor('green');
  
  
  function PrototypeColor(name) {
      this._name = name;
  }
  PrototypeColor.prototype.getName = function () {
      return this._name;
  };
  PrototypeColor.prototype.setName = function (n) {
      this._name = n;
  };
  var prototypeColor = new PrototypeColor('green');

Test runner

Ready to run.

Testing in
TestOps/sec
Closure
var name = closureColor.getName();
closureColor.setName('blue');
ready
Property
var name = propertyColor.getName();
propertyColor.setName('blue');
ready
Prototype
var name = prototypeColor.getName();
prototypeColor.setName('blue');
ready

Revisions

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