Property Getter / Setter Techniques

JavaScript performance comparison

Revision 88 of this test case created by Joshua Wise

Preparation code

<script>
  var obj = {
   prop: 0
  }
  
  var obj1 = {
   _prop: 0,
   getProp: function() {
    return this._prop;
   },
   setProp: function(value) {
    this._prop = value;
   }
  };
  
  var obj2 = {
   _prop: 0,
   get prop() {
    return this._prop;
   },
   set prop(value) {
    this._prop = value;
   },
  };
  
  
  var obj3 = {
   _prop: 0
  }
  
  
  Object.defineProperty(obj3, "prop", {
   get: function() {
    return this._prop;
  
   },
   set: function(val) {
    this._prop = val;
   }
  });
  
  var obj4 = {
   _prop: 0
  }
  
  
  obj4.__defineGetter__("prop", function() {
   return this._prop;
  });
  
  obj4.__defineSetter__("prop", function(val) {
   this._prop = 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
Method Get
obj1.getProp();
pending…
get
obj2.prop;
pending…
Object.defineProperty Get
obj3.prop;
pending…
__defineGetter__ Get
obj4.prop;
pending…
Property Get
obj.prop;
pending…
Method Set
obj1.setProp(1);
pending…
set
obj2.prop = 1;
pending…
Object.defineProperty Set
obj3.prop = 1;
pending…
__defineSetter__ Set
obj4.prop = 1;
pending…
Property Set
obj.prop = 1;
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.

0 Comments