getter method private and public
JavaScript performance comparison
Info
constructing objects with getters that access a public property is faster than objects with getters inside a closure, but is that also the case if you run the getter method many times?
Preparation code
<script>
Benchmark.prototype.setup = function() {
function PrivateObject( item ) {
this.getItem = function() {
return item;
};
}
function PublicObject( item ) {
this._item = item;
}
PublicObject.prototype.getItem = function() {
return this._item;
};
item1 = new PrivateObject( 1337 );
item2 = new PublicObject( 1337 );
};
</script>
Test runner
Warning! For accurate results, please disable Firebug before running the tests. (Why?)
Java applet disabled.
| Test | Ops/sec | |
|---|---|---|
getter private |
|
pending… |
getter public |
|
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 jurassicPieter
- Revision 3: published by Tomás
0 comments