Object.defineProperty vs __defineGetter__ vs normal

时间:2023-11-17 08:27:50
Testing in Chrome 31.0.1650.63 32-bit on Windows Server 2008 R2 / 7 64-bit
Test Ops/sec
Object.defineProperty

for (var i = 0; i < 1000; i++) { obj = {};
Object.defineProperty(obj, "x", {
 get: funct['get'],
 set: funct['set']
})  obj.x = i;
 acc += obj.x;
}
ready
__defineGetter__
for (var i = 0; i < 1000; i++) {
obj = {};
obj.__defineGetter__('x', funct['get']);
obj.__defineSetter__('x', funct['set']);  obj.x = i;
 acc += obj.x;
}
ready
Normal
for (var i = 0; i < 1000; i++) {
obj = {};
 obj._x = i;
 acc += obj._x;
}
ready
Prototype
for (var i = 0; i < 1000; i++) {
obj = {};
obj.prototype = {
 get x() {
  return this._x;
 }, set x(value) {
  this_x = value;
 }
}  obj.x = i;
 acc += obj.x;
}
ready
Setter
for (var i = 0; i < 1000; i++) {
obj = {};
obj.setX = function(value) {
 this._x = value;
}  obj.setX(i);
 acc += obj._x;
}
ready
defineProperty with local funcs
for (var i = 0; i < 1000; i++) {
obj = {};
Object.defineProperty(obj, "x", {
 get: function() { return this._x; },
 set: function(v) { this._x = v; },
 configurable: true
})  obj.x = i;
 acc += obj.x;
}
ready

http://jsperf.com/object-defineproperty-vs-definegetter-vs-normal/49