Property Getter / Setter Techniques
JavaScript performance comparison
Info
Testing various techniques for creating getters / setters in JavaScript.
Preparation code
<script>
var obj = {
_prop: 0
}
var obj1 = {
_prop: 0,
getProp: function() {
return this._prop;
},
setProp: function(value) {
this._prop = value;
}
};
var obj2 = {
_prop: 0,
get prop() {
return this._prop;
},
set prop(value) {
this._prop = value;
},
};
var obj3 = {
_prop: 0
}
Object.defineProperty(obj3, "prop", {
get: function() {
return this._prop;
},
set: function(val) {
this._prop = val;
}
});
var obj4 = {
_prop: 0
}
obj4.__defineGetter__("_prop", function() {
return this._prop;
});
obj4.__defineSetter__("_prop", function(val) {
this._prop = val;
});
</script>
Test runner
Warning! For accurate results, please disable Firebug before running the tests. (Why?)
Java applet disabled.
| Test | Ops/sec | |
|---|---|---|
Methods |
|
pending… |
get / set |
|
pending… |
Object.defineProperty |
|
pending… |
__defineGetter__ |
|
pending… |
Property |
|
pending… |
Compare results of other browsers
Revisions
You can edit these tests or add even more tests to this page by appending /edit to the URL. Here’s a list of current revisions for this page:
- Revision 1: published by Mike Chambers
- Revision 3: published by Miller Medeiros
- Revision 6: published
- Revision 7: published by Scott Wolchok
- Revision 8: published by john
- Revision 10: published
- Revision 11: published by Stéphan Kochen
- Revision 12: published
- Revision 14: published by Elijah Insua
- Revision 16: published
- Revision 18: published
- Revision 19: published by Andrew Petersen
- Revision 20: published
- Revision 21: published
- Revision 22: published by ppold
- Revision 23: published by ppold
- Revision 24: published
0 comments