ES5 getters/setters versus getter/setter methods

JavaScript performance comparison

Revision 4 of this test case created by Jason Feinstein

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;
   }
  };
</script>

Test runner

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

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
ES5 way
obj.prop = obj.prop + 1;
pending…
Using methods
obj.setProp(obj.getProp() + 1);
pending…
+= With Getters/setters
obj.prop += 1;
pending…
++ with getters/setters
obj.prop++;
pending…
Just Getting w/ Getters
obj.prop;
pending…
Just Getting w/ Methods
obj.getProp();
pending…
Just Setting w/ Setters
obj.prop = 5;
pending…
Just Setting w/ Methods
obj.setProp(5);
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. Here’s a list of current revisions for this page:

0 comments

Add a comment