DefineProperty vs regular property

JavaScript performance comparison

Revision 5 of this test case created

Preparation code

<script type="text/javascript">
  var definedProperty = function () {
    Object.defineProperty(this, "prop", {
      configurable: true, enumerable: true, value: 1
    });
  }

  var definedConstantProperty = function () {
    var value = 1;
    Object.defineProperty(this, "prop", {
      set: function(x){ value = x; },
      get: function(){ return value}
    });
  }

  var definedValueOnlyProperty = function () {
    Object.defineProperty(this, "prop", {
      value: 1,
    });
  }

  var regularProperty = function () {
    this.prop = 1;
  }
 
  var count = 50000;
  var defineds = new Array(count);
  var definedConsts = new Array(count);
  var definedValues = new Array(count);
  var regulars = new Array(count);

  for (var i = 0; i < count; i++) {
    defineds[i] = new definedProperty();
    definedConsts[i] = new definedConstantProperty();
    definedValues[i] = new definedValueOnlyProperty();
    regulars[i] = new regularProperty();
  }
</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
defineProperty
var sum = 0;
for (var i = 0, a = defineds; i < count; i++) {
  sum += a[i].prop;
}
pending…
regular property
var sum = 0;
for (var i = 0, a = regulars; i < count; i++) {
  sum += a[i].prop;
}
pending…
defineProperty with writable=false
var sum = 0;
for (var i = 0, a = definedConsts; i < count; i++) {
  sum += a[i].prop;
}
pending…
defineProperty with only value specified
var sum = 0;
for (var i = 0, a = definedValues; i < count; i++) {
  sum += a[i].prop;
}
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