getters

JavaScript performance comparison

Test case created

Info

Testing the difference between a getFoo() method, "get foo" and Object.defineProperties(o, "foo": { get: function() })

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var o = {
      _foo: 42,
      getFoo: function () { return this._foo; },
      get foo() {return this._foo;}
    };
   
    Object.defineProperties(o, {
      "fooProp": { get: function () { return this._foo; } }
    });
   
     
};
</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
getFoo()
var sum = 0;

for (var i = 0; i < 10000; i++) {
  sum += o.getFoo();
}
console.log("Result of o.getFoo(): " + sum);
pending…
get foo
var sum = 0;

for (var i = 0; i < 10000; i++) {
  sum += o.foo;
}
console.log("Result of o.foo: " + sum);
pending…
fooProp
var sum = 0;

for (var i = 0; i < 10000; i++) {
  sum += o.fooProp;
}
console.log("Result of o.fooProp: " + sum);
pending…

You can edit these tests or add even more tests to this page by appending /edit to the URL.

Compare results of other browsers

0 comments

Add a comment