Object.create vs Crockford vs Constructor (v22)

Revision 22 of this benchmark created by Mark Bradley on


Description

Adding a slower constructor function that can be used with or without the new operator.

Preparation HTML

<script>
  var sharedPrototype = {
   one: function() {
    return 1;
   },
   two: function() {
    return 2;
   },
   three: function() {
    return 3;
   }
  };
  
  var crockfordCreate = (function() {
   var f = function() {};
   return function(proto){
    f.prototype = proto;
    return new f();
   }
  })();
  
  var Constructor = function() {};
  Constructor.prototype = sharedPrototype;
  
  var SafeConstructor = function() {
    var win = (function() { return this; })();
    var that = this;
    if (that === win) {
      return new SafeConstructor();
    }
  };
  SafeConstructor.prototype = sharedPrototype;
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
Object.create()
var i = 100;
while (i--) Object.create(sharedPrototype);
ready
Crockford Create
var i = 100;
while (i--) crockfordCreate(sharedPrototype);
ready
Constructor
var i = 100;
while (i--) new Constructor;
ready
SafeConstructor
var i = 100;
while (i--) new SafeConstructor;
ready

Revisions

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