Object.defineProperty vs __defineGetter__ vs normal (v55)

Revision 55 of this benchmark created by Cameron Foale on


Description

Test different access method to an object property.

Preparation HTML

<script>


  obj = {
   _x: 0
  }

  pobj = {
   _x: 0
  }

  pobj.prototype = {};
    
  funct = {
   get: function() {
    return this._x
   },
   set: function(value) {
    this._x = value
   }
  }

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

Object.defineProperty(obj, "nx", {
 get: funct['get'],
 set: funct['set'],
 configurable: true,
 enumerable: true,
})

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

Object.defineProperty(pobj.prototype, "nx", {
 get: funct['get'],
 set: funct['set'],
 configurable: true,
 enumerable: true
})

obj.__defineGetter__('y', funct['get']);
obj.__defineSetter__('y', funct['set']);

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

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

  function check() {
    // sum of 1-1000
    // if(console && console.assert) {
    //  console.assert(acc === 500500, 'acc should be 500500');
    // }
  }
</script>

Setup

obj._x = 0;
    pobj._x = 0;
    i = 1000;

Test runner

Ready to run.

Testing in
TestOps/sec
Object.defineProperty
acc = 0;
for(var i = 1000; i > 0; --i)
{
 obj.x = i;
 acc += obj.x;
}
check();
ready
__defineGetter__
acc = 0;
for(var i = 1000; i > 0; --i)
{
 obj.y = i;
 acc += obj.y;
}
check();
ready
Normal
acc = 0;
for(var i = 1000; i > 0; --i)
{
 obj._x = i;
 acc += obj._x;
}
check();
ready
Prototype
acc = 0;
for(var i = 1000; i > 0; --i)
{
 obj.z = i;
 acc += obj.z;
}
check();
ready
Setter
acc = 0;
for(var i = 1000; i > 0; --i)
{
 obj.setX(i);
 acc += obj._x;
}
check();
ready
Object.defineProperty configurable
acc = 0;
for(var i = 1000; i > 0; --i)
{
 obj.nx = i;
 acc += obj.nx;
}
check();
ready
Object.defineProperty on proto
acc = 0;
for(var i = 1000; i > 0; --i)
{
 pobj.x = i;
 acc += pobj.x;
}
check();
ready
Object.defineProperty on proto, configurable, writable
acc = 0;
for(var i = 1000; i > 0; --i)
{
 pobj.nx = i;
 acc += pobj.nx;
}
check();
ready

Revisions

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