Property Getter / Setter Techniques (v74)

Revision 74 of this benchmark created on


Description

Testing various techniques for creating getters / setters in JavaScript.

Preparation HTML

<script>
  var global = this;
  
  (function() {
    global.obj = {
      _prop: 0
    };
  
    global.obj1 = {
      _prop: 0,
      getProp: function() {
        return this._prop;
      },
      setProp: function(value) {
        this._prop = value;
      }
    };
  
    global.obj2 = {
      _prop: 0,
      get prop() {
        return this._prop;
      },
      set prop(value) {
        this._prop = value;
      }
    };
  
  
    global.obj3 = {
      _prop: 0
    }
  
  
    Object.defineProperty(global.obj3, "prop", {
      get: function() {
        return this._prop;
  
      },
      set: function(val) {
        this._prop = val;
      }
    });
  
    global.obj4 = {
      _prop: 0
    }
  
  
    global.obj4.__defineGetter__("prop", function() {
      return this._prop;
    });
  
    global.obj4.__defineSetter__("prop", function(val) {
      this._prop = val;
    });
    document.body._prop = 0;
document.body.setAttribute('_attr', 0);
this._prop=0;
  })();
</script>

Setup

global.obj = {
    prop: 0
    };

Test runner

Ready to run.

Testing in
TestOps/sec
Methods
global.obj1.setProp(global.obj1.getProp() + 1);
ready
get / set
global.obj2.prop = global.obj2.prop + 1;
ready
Object.defineProperty
global.obj3.prop = global.obj3.prop + 1;
ready
__defineGetter__
global.obj4.prop = global.obj4.prop + 1;
ready
Property
global.obj.prop = global.obj.prop + 1;
ready
Element Prop
document.body._prop = document.body._prop + 1;
ready
Element Attributes
document.body.attributes['_attr'] = document.body.attributes['_attr'] + 1;
ready
setAttribute
document.body.setAttribute('_attr', document.body.getAttribute('_attr') + 1);
ready
this.prop
this._prop = this._prop + 1;
ready
window.prop
window.obj._prop = window.obj._prop + 1;
ready
FAULT
window.OBJ = WINDOW.OBEN.S;
ready

Revisions

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