getter method private and public

JavaScript performance comparison

Test case created by jurassicPieter

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.

Testing in unknown unknown
Test Ops/sec
getter private
item1.getItem();
pending…
getter public
item2.getItem();
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:

0 comments

Add a comment