getters/setters vs properties (v2)

Revision 2 of this benchmark created on


Description

Just curious about performance of getters / setters vs classic properties.

Preparation HTML

<script>
  var Point1 = function(x, y) {
      this._x = x;
      this._y = y;
      };
  
  Object.defineProperties(Point1.prototype, {
    x: {
      get: function() {
        return this._x;
      },
      set: function(v) {
        this._x = v;
      },
      enumerable: true
    },
    y: {
      get: function() {
        return this._y;
      },
      set: function(v) {
        this._y = v;
      },
      enumerable: true
    },
    _x: {
      value: 0
    },
    _y: {
      value: 0
    }
  });
  
  var Point2 = function(x, y) {
  
      this.x = x;
      this.y = y;
  
      }
      
      
      
  var Point3 = function(x, y) {
  
      this.x = x;
      this.y = y;
  
      this.getX = function() {
        return this.x;
      }
  
      this.setX = function(x) {
        this.x = x;
      }
  
      this.getY = function() {
        return this.y;
      }
  
      this.setY = function(y) {
        this.y = y;
      }
  
      }
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
getters / setters
var p = new Point1(0, 0);
p.x = 1;
p.y = p.x;
ready
properties
var p = new Point2(0, 0);
p.x = 1;
p.y = p.x;
ready
custom getters / setters
var p = new Point3(0, 0);
p.setX(1);
p.setY(p.getX());
ready

Revisions

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