jsPerf.app is an online JavaScript performance benchmark test runner & jsperf.com mirror. It is a complete rewrite in homage to the once excellent jsperf.com now with hopefully a more modern & maintainable codebase.
jsperf.com URLs are mirrored at the same path, e.g:
https://jsperf.com/negative-modulo/2
Can be accessed at:
https://jsperf.app/negative-modulo/2
Testing various techniques for creating getters / setters in JavaScript. This revision uses actual prototype-based classes where possible; seems to result in a significant speedup for the method-based version.
<script>
function BareProperty() {
this.prop = 0;
}
function GetterAndSetter() {
this._prop = 0;
}
GetterAndSetter.prototype.getProp = function() {
return this._prop;
};
GetterAndSetter.prototype.setProp = function(value) {
this._prop = value;
};
function GetSet() {
this._prop = 0;
}
GetSet.prototype = {
get prop() {
return this._prop;
}, set prop(value) {
this._prop = value;
},
};
function DefineProperty() {
this._prop = 0;
Object.defineProperty(this, "prop", {
get: function() {
return this._prop;
},
set: function(val) {
this._prop = val;
}
});
}
function DefineGetterSetter() {
this._prop = 0;
this.__defineGetter__("prop", function() {
return this._prop;
});
this.__defineSetter__("prop", function(val) {
this._prop = val;
});
}
</script>
var obj = new BareProperty();
var obj1 = new GetterAndSetter();
var obj2 = new GetSet();
var obj3 = new DefineProperty();
var obj4 = new DefineGetterSetter();
Ready to run.
Test | Ops/sec | |
---|---|---|
Methods |
| ready |
get / set |
| ready |
Object.defineProperty |
| ready |
__defineGetter__ |
| ready |
Property |
| ready |
You can edit these tests or add more tests to this page by appending /edit to the URL.