Object.defineProperty vs __defineGetter__ vs normal (v60)

Revision 60 of this benchmark created on


Description

Test different access method to an object property

Preparation HTML

<script>
var funct = {
   get: function() {
    return this._x
   },
   set: function(value) {
    this._x = value
   }
  }
var es5 = {
   _x: 0
  }
Object.defineProperty(es5, "x", {
 get: funct['get'],
 set: funct['set']
});

function es5ctor() {
  this._x = 0;
}
es5ctor.prototype = {};
Object.defineProperty(es5ctor.prototype, "x", {
 get: funct['get'],
 set: funct['set']
});
es5constructed = new es5ctor();

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

var es3 = {
  _x : 0,
  getX: funct.get,
  setX: funct.set
}

var obj = {x:0};
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
Object.defineProperty
a = []
for (var i = 0; i < 1000; i++)
    a.push(es5.x)
ready
__defineGetter__
a = []
for (var i = 0; i < 1000; i++)
    a.push(es5ish.x)
ready
Normal
a = []
for (var i = 0; i < 1000; i++)
    a.push(obj.x)
ready
es3
a = []
for (var i = 0; i < 1000; i++)
    a.push(es3.getX())
ready
Object.defineProperty on the prototype
a = []
for (var i = 0; i < 1000; i++)
    a.push(es5constructed.x)
ready

Revisions

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