Object.create() vs. constructor vs. object literal

JavaScript performance comparison

Revision 10 of this test case created

Preparation code

<script>
  var A = function() {
      this._x = 0;
      };
 
  var B = function() {
      this._z = 0;
      };
</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
Object.create() using in-place property object
A.prototype = Object.create(Object.prototype, {
  x: {
    get: function() {
      return this._x;
    },
    set: function(v) {
      this._x = v;
    }
  }
});

B.prototype = Object.create(A.prototype, {
  foo: {
    value: function() {;
    }
  }
});

var b = new B();
pending…
Object.create() using pre-defined property object
var _x = {
  x: {
    get: function() {
      return this._x;
    },
    set: function(v) {
      this._x = v;
    }
  }
};

var _foo = function() {;
    };

A.prototype = Object.create(Object.prototype, {
  x: _x
});

B.prototype = Object.create(A.prototype, {
  foo: {
    value: _foo
  }
});

var b = new B();
pending…
Constructor function
A.prototype = {
  get: function() {
    return this._x;
  },
  set: function(v) {
    this._x = v;
  }
};

B.prototype = new A();
B.prototype.constructor = B;

B.prototype.foo = function() {;
};

var b = new B();
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