DefineProperty vs regular property

JavaScript performance comparison

Revision 4 of this test case created

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
      var definedProperty = function () {
        Object.defineProperty(this, "prop", {
          configurable: true,
          enumerable: true,
          writable: true,
          value: 4
        });
      }
   
      var definedConstantProperty = function () {
        Object.defineProperty(this, "prop", {
          configurable: true,
          enumerable: true,
          writable: false,
          value: 4
        });
      }
   
      var regularProperty = function () {
        this.prop = 4;
      }
     
      var count = 500;
      var defineds = new Array(count);
      var definedConsts = new Array(count);
      var regulars = new Array(count);
   
      for (var i = 0; i < count; i++) {
        defineds[i] = new definedProperty();
        definedConsts[i] = new definedConstantProperty();
        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…

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