ES5 getters/setters versus getter/setter methods

JavaScript performance comparison

Revision 10 of this test case created by

Preparation code

<script>
  var obj = {
   _prop: 0,
   get prop() {
    return this._prop;
   },
   set prop(value) {
    this._prop = value
   },
   getProp: function() {
    return this._prop;
   },
   setProp: function(value) {
    this._prop = value;
   }
  };

Object.defineProperty(obj, "pp", {
    get: function () {
        return this._prop;
    },
    set: function (x) {
        this._prop = x;
    },
    enumerable: false,
    configurable: false
});
</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
ES5 way
obj.prop = obj.prop + 1;
pending…
Using methods
obj.setProp(obj.getProp() + 1);
pending…
Using defineProperty()
obj.pp = obj.pp + 1;
pending…
assignment
obj._prop = 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