Direct access vs setter vs defineProperty

JavaScript performance comparison

Test case created by Johannes Stein

Preparation code


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

};
</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 = 6;
pending…
Setter
obj2.prop = 6;
pending…
defineProperty
obj3.prop = 6;
pending…

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

0 Comments