Object.defineProperty vs __defineGetter__ vs normal vs private getter setter (v40)

Revision 40 of this benchmark created by test on


Description

Test different access method to an object property

Preparation HTML

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

Test runner

Ready to run.

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

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

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

for (var i = 0; i < 1000; i++) {
 obj.setX(i);
 acc += obj._x;
}
ready
private
function Obj(){
    var x;   
    this.__defineGetter__("x", function(){
        return x;
    });
   
    this.__defineSetter__("x", function(_x){
        x = _x;
    });
}
var obj = new Obj()
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.