Object.defineProperty vs. others (v42)

Revision 42 of this benchmark created by yzsolt on


Description

Test different access method to an object property

Setup

var obj = function() {
       this._x = 0;
      };
      
      var acc = 0;
      
      funct = {
       get: function() {
        return this._x
       },
       set: function(value) {
        this._x = value
       }
      }

Test runner

Ready to run.

Testing in
TestOps/sec
defineProperty WRITABLE
Object.defineProperty(obj, "x", {
 get: funct['get'],
 set: funct['set']
})

for (var i = 0; i < 1000; i++) {
 obj.x = i;
 acc += obj.x;
}
ready
__defineG/Setter__
obj.__defineGetter__('x', funct['get']);
obj.__defineSetter__('x', funct['set']);

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
getter/setter
obj.setX = function(value) {
 this._x = value;
}

obj.getX = function() {
 return this._x;
}

for (var i = 0; i < 1000; i++) {
 obj.setX(i);
 acc += obj.getX();
}
ready
private
obj.__defineGetter__('x', function() { return this._x; });
obj.__defineSetter__('x', function(value) { this._x = value; });

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

Revisions

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