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

JavaScript performance comparison

Revision 40 of this test case created by ju1ius

Preparation code

<script>
  Obj = function() {
   this.p = 1;
   this.foo = "foo";
   var bar = "bar";
   this.getBar = function(){ return bar; }
  };
  Obj.prototype.getFoo = function(){ return this.foo; };

  propObj = {
   p: {value: 1},
   foo: {value: "foo"},
   bar: {value: "bar"},
   getFoo: {value: function(){ return this.foo; }},
   getBar: {value: function(){ return this.bar; }}
  };
</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
o = Object.create(Object.prototype, {p: {value: 1}});
pending…
Object.create() using pre-defined property object
o = Object.create(Object.prototype, propObj);
pending…
Constructor function
o = new Obj();
pending…
Simple version
o = {
   p: 1,
   foo: "foo",
   bar: "bar",
   getFoo: function(){ return this.foo; },
   getBar: function(){ return this.bar; }
};
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