Object.defineProperty vs __defineGetter__ vs normal (v69)

Revision 69 of this benchmark created 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
   }
  }

  class_gs = function() {
   this._x = 0;
  };

  obj_dp = {
   _x: 0
  }

Object.defineProperty(obj_dp, "x", {
  get: funct['get'],
  set: funct['set']
})

class_gs.prototype = {
  get x() {
    return this._x;
  },

  set x(v) {
    this._x = v;
  }
};

  obj_gs = new class_gs();

</script>

Test runner

Ready to run.

Testing in
TestOps/sec
Object.defineProperty
for (var i = 0; i < 1000; i++) {
  obj_dp.x = i;
  acc += obj_dp.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
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
set / get functions
for (var i = 0; i < 1000; i++) {
  obj_gs.x = i;
  acc += obj_gs.x;
}
ready

Revisions

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