Direct property vs getter vs defineProperty

JavaScript performance comparison

Test case created by Johannes Stein

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  var obj1 = {
    prop: 5
  };
  
  var obj2 = {
    get prop() {
      return 5;
    }
  }
  
  var obj3 = {};
  Object.defineProperty(obj3, 'prop', {
    get: function() { return 5; }
  })

};
</script>

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
Direct access
obj1.prop === 5
pending…
Getter
obj2.prop === 5
pending…
defineProperty
obj3.prop === 5
pending…

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

0 Comments