Creating new objects

JavaScript performance comparison

Test case created by Ahmad Bamieh

Preparation code

<script>
  var sharedPrototype = {
   one: function() {
    return 1;
   },
   two: function() {
    return 2;
   },
   three: function() {
    return 3;
   }
  };
  
  var rodrigoCreate = function () {
      return new Constructor;
  };
  
  var crockfordCreate = function(proto) {
   var f = function() {};
   f.prototype = proto;
   return new f();
  };
  
  var Constructor = function(a) {
    this.foo = a;
  };
  Constructor.prototype = sharedPrototype;
  
  var cornfordCreate = (function() {
   var f = function() {};
   return function(proto) {
    f.prototype = proto;
    return new f();
   };
  })();
</script>
    

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
Object.create()
Object.create(sharedPrototype);
pending…
Crockford Create
crockfordCreate(sharedPrototype);
pending…
Constructor
new Constructor;
pending…
Proto
({
  '__proto__': Constructor
 })
pending…
Cornford Create
cornfordCreate(sharedPrototype);
pending…
Rodrigo Create
rodrigoCreate();
pending…
Object.setPrototypeOf
Object.setPrototypeOf({}, sharedPrototype)
pending…

You can edit these tests or add even more tests to this page by appending /edit to the URL.

1 Comment

Ahmad Bamieh (revision owner) commented :

Tests the creation of new objects, based on https://jsperf.com/object-create-vs-crockford-vs-jorge-vs-constructor/64 but jsperf is a little buggy in the edit mode, so i had to create a new one and apply my changes.

added Object.setPrototypeOf test made the comparison fair between rodrigoCreate and new Constructor test cases by removing the passed params when calling new Constructor in both tests.