Object descriptor getter/setter performance in recent Chrome/V8 versions
Solution 1:
The changes in performance are relevant to this Chromium issue (credits go to @VyacheslavEgorov).
To avoid performance issues, a prototype should be used instead. This is one of few reasons why singleton classes may be used to instantiate an object once.
With ES5:
var _a = 1;
function Obj() {}
Object.defineProperty(Obj.prototype, 'a', {
enumerable: true,
get: function () {
return _a;
},
set: function(val) {
_a = val;
}
});
var obj = new Obj();
// or
var obj = Object.create(Obj.prototype);
Or with ES6 syntactic sugar:
class Obj {
constructor() {
this._a = 1;
}
get a() {
return this._a;
}
set a(val) {
this._a = val;
}
}
let obj = new Obj();