creation of objects with getters and setters

JavaScript performance comparison

Revision 14 of this test case created by dood

Preparation code

<script>
var objCount = 2000
</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
naive way
for (var i = 0; i < objCount; i++) {
  var a = {
    _prop: 0,
    get prop() {
      return this._prop
    },
    set prop(v) {
      return this._prop = v
    }
  }
}
pending…
make method in prototype
var agent = function() {
  this.prop = 0
}
agent.prototype.setProp = function(v) {
  this.prop = v
}
for (var i = 0; i < objCount; i++) {
  var a = new agent()
}
pending…
basic
var agent = function() {
  this.prop = 0
}
for (var i = 0; i < objCount; i++) {
  var a = new agent()
}
pending…
Using methods
var agent = function() {
  this.prop = 0
  this.setProp = function(value) {
    this.prop = value
  }
}
for (var i = 0; i < objCount; i++) {
  var a = new agent()
}
pending…
making objects with getters and setters in prototype
var agent = function() {
  this._prop = 0
}

agent.prototype = {
  get prop() {
    return this._prop
  },
  set prop(v) {
    return this._prop = v
  }
}

for (var i = 0; i < objCount; i++) {
  var a = new agent()
}
pending…
using define property in prototype
var agent = function() {
  this._prop = 0
}

var tmpobj = {}
Object.defineProperty(tmpobj, "prop", {
  get: function() {
    return this._prop
  },
  set: function(x) {
    this._prop = x
  },
  enumerable: false,
  configurable: false
});
agent.prototype = tmpobj

for (var i = 0; i < objCount; i++) {
  var a = new agent()
}
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