Object.defineProperty vs __defineGetter__ vs normal (v27)

Revision 27 of this benchmark created on


Description

Test different access method to an object property

Preparation HTML

<script>
  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
var obj = {_x:0}
Object.defineProperty(obj, "x", {
 get: funct['get'],
 set: funct['set']
})
acc = 0;
var check = 0;

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

if(acc !== check) { throw new Error('Didn\'t work correctly!'); }

 
ready
__defineGetter__
var obj = {_x:0}
obj.__defineGetter__('x', funct['get']);
obj.__defineSetter__('x', funct['set']);

acc = 0;
var check = 0;

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

if(acc !== check) { throw new Error('Didn\'t work correctly!'); }

 
ready
Inline
var obj = {
 _x:0,
 get x() {
  return this._x;
 },
 set x(value) {
  this._x = value;
 }
}

acc = 0;
var check = 0;

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

if(acc !== check) { throw new Error('Didn\'t work correctly!'); }

 
ready
Object Function
var obj = {
  _x:0,
  getX:function() { return this._x; },
  setX:function(x) { this._x = x; }
};

acc = 0;
var check = 0;

for (var i = 0; i < 10000; i++) {
 obj.setX(i);
 acc += obj.getX();
 check += i;
}

if(acc !== check) { throw new Error('Didn\'t work correctly!'); }
 
ready
Function
var x = 0;
function getX() { return x; }
function setX(value) {x = value;}

acc = 0;
var check = 0;

for (var i = 0; i < 10000; i++) {
 setX(i);
 acc += getX();
 check += i;
}

if(acc !== check) { throw new Error('Didn\'t work correctly!'); }
 
ready
Direct
var obj = {x:0}
acc = 0;
var check = 0;

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

if(acc !== check) { throw new Error('Didn\'t work correctly!'); }
 
ready

Revisions

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