Object.defineProperty vs __defineGetter__ vs normal (v63)

Revision 63 of this benchmark created on


Description

Test different access method to an object property

Preparation HTML

<script>
obj = { _x: 0, _y: 0, _z: 0 };
  
var acc = 0;

obj.__defineGetter__('x', function() { return this._x; });
obj.__defineSetter__('x', function(v) { this._x = v; });

Object.defineProperty(obj, "y", {
 configurable: true,
 get: function() { return this._y; },
 set: function(v) { this._y = v; }
})

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

obj.prototype = {
 get z() {
  return this._z;
 },
 
 set z(value) {
  this._z = value;
 }
}

</script>

Test runner

Ready to run.

Testing in
TestOps/sec
__defineGetter__
for (var i = 0; i < 1000; i++) {
 obj.x = i;
 acc += obj.x;
}
ready
Object.defineProperty
for (var i = 0; i < 1000; i++) {
 obj.y = i;
 acc += obj.y;
}
ready
Normal
for (var i = 0; i < 1000; i++) {
 obj._x = i;
 acc += obj._x;
}
ready
Prototype
for (var i = 0; i < 1000; i++) {
 obj.z = i;
 acc += obj.z;
}
ready
Setter
for (var i = 0; i < 1000; i++) {
 obj.setX(i);
 acc += obj._x;
}
ready

Revisions

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