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

JavaScript performance comparison

Revision 129 of this test case created

Preparation code

<script>
  function test() {
    return true;
  }

  Obj = function(r) {
   this.p = 1;
   this.q = 'hello';
   this.r = r;
  };

  Obj2 = function() {};
  Obj2.prototype = new Obj();

  protoObj = {
    p: 1,
    q: 'hello',
    r: ''
  };

  function protoFactory (r) {
    var obj = Object.create(protoObj);
    obj.r = r;
    if ( test() ) {
      obj.s = 'passed'
    }
    return obj;
  };
 
  propObj = {
   p: {
    value: 1,
    q: 'hello',
    r: 'test'
   }
  };
</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,
    q: 'hello',
    r: 'test'
  }
});
if ( test() ) {
  o.s = 'passed';
}
pending…
Object.create() using pre-defined property object
o = Object.create(Object.prototype, propObj);
if ( test() ) {
  o.s = 'passed';
}
pending…
Constructor function with new
o = new Obj('test');
 
pending…
Literal
o = {
  p: 1,
  q: 'hello',
  r: 'test',
  s: 'passed'
}
pending…
Constructor function without new
o = Obj('test');
pending…
Object.create without prop
o = Object.create(protoObj);
o.r = 'test';
if ( test() ) {
  o.s = 'passed';
}
pending…
Object.create Factory
o = protoFactory('test');
pending…
Constructor function with new/prototype
o = new Obj2('test');
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