Define property vs define properties (v7)

Revision 7 of this benchmark created by devu on


Description

Based on: Checking Object.defineProperty against Object.defineProperties in a 'realistic' code environment.

Combined with calls using those methods

Setup

var defineP = Object.defineProperty;

Test runner

Ready to run.

Testing in
TestOps/sec
defineProperty
var objA = function() {};
Object.defineProperty(objA.prototype, 'width', {
  get: function() {
    return this.width;
  },
  set: function(v) {
    this.width = v;
  }
});
Object.defineProperty(objA.prototype, 'height', {
  get: function() {
    return this.height;
  },
  set: function(v) {
    this.height = v;
  }
});
Object.defineProperty(objA.prototype, 'area', {
  get: function() {
    return this.area;
  },
  set: function(v) {
    this.area = area;
  }
});

objA.width = 10;
objA.height = 10;
objA.area = 0;

for (var i = 0; i < 10; i++) {
  var w = objA.width;
  var h = objA.height;
  objA.area = w * h;
}
ready
defineProperties
var objB = function() {};
Object.defineProperties(objB.prototype, {
  width: {
    get: function() {
      return this.width;
    },
    set: function(v) {
      this.width = v;
    }
  },
  height: {
    get: function() {
      return this.height;
    },
    set: function(v) {
      this.height = v;
    }
  },
  area: {
    get: function() {
      return this.area;
    },
    set: function(v) {
      this.area = v;
    }
  }
});

objB.width = 10;
objB.height = 10;
objB.area = 0;

for (var i = 0; i < 10; i++) {
  var w = objB.width;
  var h = objB.height;
  objB.area = w * h;
}
ready
getter
var objC = function() {};
objC.prototype = {
  get width() {
    return this.width;
  },
  set width(v) {
    this.width = v;
  },
  get height() {
    return this.height;
  },
  set height(v) {
    this.height = v;
  },
  get area() {
    return this.height;
  },
  set area(v) {
    this.height = v;
  }
}

objC.width = 10;
objC.height = 10;
objC.area = 0;

for (var i = 0; i < 10; i++) {
  var w = objC.width;
  var h = objC.height;
  objC.area = w * h;
}
ready
shorthand
var objD = function() {};
defineP(objD.prototype, 'width', {
  get: function() {
    return this.width;
  },
  set: function(v) {
    this.width = v;
  }
});
defineP(objD.prototype, 'height', {
  get: function() {
    return this.height;
  },
  set: function(v) {
    this.height = v;
  }
});
defineP(objD.prototype, 'area', {
  get: function() {
    return this.area;
  },
  set: function(v) {
    this.area = area;
  }
});

objD.width = 10;
objD.height = 10;
objD.area = 0;

for (var i = 0; i < 10; i++) {
  var w = objD.width;
  var h = objD.height;
  objD.area = w * h;
}
ready
set get object literal
var objE = {
  w: 10,
  h: 10,
  a: 0,
  getWidth: function() {
    return this.w;
  },
  setWidth: function(v) {
    this.w = v;
  },
  getHeight: function() {
    return this.h;
  },
  setHeight: function(v) {
    this.h = v;
  },
  setArea: function() {
    return this.a;
  },
  getArea: function(v) {
    this.a = v;
  }
};

for (var i = 0; i < 10; i++) {
  var w = objE.getWidth();
  var h = objE.getHeight();
  objE.setArea(w * h);
}
ready
function
var objF = (function() {
  var self = this;
  var w = 10;
  var h = 10;
  var a = 0;

  self.getWidth = function() {
    return w;
  }
  self.setWidth = function(v) {
    w = v;
  }
  self.getHeight = function() {
    return h;
  }
  self.setHeight = function(v) {
    h = v;
  }
  self.setArea = function() {
    return a;
  },
  self.getArea = function(v) {
    a = v;
  }
  return self;
})();

for (var i = 0; i < 10; i++) {
  var w = objF.getWidth();
  var h = objF.getHeight();
  objF.setArea(w * h);
}
ready

Revisions

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