Object.defineProperty vs __defineGetter__ vs normal (v19)

Revision 19 of this benchmark created on


Description

Test different access method to an object property.

Test both getting and setting.

Setup

obj = {
        _x: 0
      }
    
      var acc = 0;
    
    
        function getVal () {
          return this._x
        }
    
        function setVal(value) {
          this._x = value
        }

Test runner

Ready to run.

Testing in
TestOps/sec
Object.defineProperty
Object.defineProperty(obj, "x", {
  get: getVal,
  set: setVal
})
for (var i = 0; i < 1000; i++) {
  obj.x = i;
  acc += obj.x;
}
ready
__defineSetter__
obj.__defineGetter__('x', getVal);
obj.__defineSetter__('x', setVal);

for (var i = 0; i < 1000; i++) {
  obj.x = i;
  acc += obj.x;
}
ready
Normal
for (var i = 0; i < 1000; i++) {
  obj._x = i;
  acc += obj._x;
}
ready
Prototype
obj.prototype = {
  get x() {
    return this._x;
  }, set x(value) {
    this._x = value;
  }
}

for (var i = 0; i < 1000; i++) {
  obj.x = i;
  acc += obj.x;
}
ready
Setter
obj.getX = function() {
 return this._x;
}

obj.setX = function(value) {
 this._x = value;
}

for (var i = 0; i < 1000; i++) {
 obj.setX(i);
 acc += obj.getX();
}
ready

Revisions

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