Object.defineProperty vs __defineGetter__ vs normal (v35)

Revision 35 of this benchmark created by Boris on


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
for (var i = 0; i < 1000; i++) es5.x += 1;
es5.x = 0;
ready
__defineGetter__
for (var i = 0; i < 1000; i++) es5ish.x += 1;
es5ish.x = 0;
ready
Normal
for (var i = 0; i < 1000; i++) obj.x += 1;
obj.x = 0;
ready
es3
for (var i = 0; i < 1000; i++) es3.setX(es3.getX() + 1);
es3.setX(0);
ready
Object.defineProperty on the prototype
for (var i = 0; i < 1000; i++) es5constructed.x += 1;
es5constructed.x = 0;
ready

Revisions

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