Object.defineProperty vs __defineGetter__ vs normal (v21)

Revision 21 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; }
      },
      obj = { _x: 0 };

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

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

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

  obj.getX = function() { return this._x; };
  obj.setX = function(value) { this._x = value; };
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
for (var i = 0; i < 1000; i++) {
 obj.a = i;
 acc += obj.a;
}
ready
Object.defineProperty
for (var i = 0; i < 1000; i++) {
 obj.b = i;
 acc += obj.b;
}
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.c = i;
 acc += obj.c;
}
ready
Setter
for (var i = 0; i < 1000; i++) {
 obj.setX(i);
 acc += obj._x;
}
ready
Function calls
for (var i = 0; i < 1000; i++) {
 funct.set.call(obj, i);
 acc += funct.get.call(obj);
}
ready
Methods
for (var i = 0; i < 1000; i++) {
 obj.setX(i);
 acc += obj.getX(i);
}
ready

Revisions

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