getter vs function prop vs prop

JavaScript performance comparison

Test case created by Nathan

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    function Test() {
   
    }
   
   
    Test.prototype.prop1 = function() {
      return (this._prop1 !== undefined ? this._prop1 = 1 : this._prop1)
    }
   
    Object.defineProperty(Test.prototype, "prop2", {
      get: function() {
        return (this._prop2 !== undefined ? this._prop2 = 1 : this._prop1)
      }
    })
   
    var test = new Test,
      a, b;
};
</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
function
a = test.prop1();
pending…
getter
b = test.prop2;
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