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

JavaScript performance comparison

Revision 118 of this test case created

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var p = {
      m1: function() {},
      m2: function() {},
      m3: function() {},
    };
   
    function C() {
      this.x = 1;
      this.y = 1;
    }
    C.prototype = p;
   
    function f() {}
};
</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
Constructor function
var o = new C();
o.x = 1;
o.y = 1;
f(o);
pending…
Object.create() without descriptors
var o = Object.create(p);
o.x = 1;
o.y = 1;
f(o);
pending…
literal
f({
  x: 1,
  y: 1,
  __proto__: p
});
pending…
literal without proto
f({
  x: 1,
  y: 1
});
pending…
array literal
f([1, 1])
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