Object.create vs Crockford vs Jorge vs Constructor vs "Safe" Constructor (v46)

Revision 46 of this benchmark created by Greg Edwards on


Description

This adds a "safe" constructor that handles cases when it is called without 'new'.

Preparation HTML

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

  var SafeConstructor = function() { if( ! (this instanceof SafeConstructor)) { return new SafeConstructor() }};
  SafeConstructor.prototype = sharedPrototype;

</script>

Test runner

Ready to run.

Testing in
TestOps/sec
Object.create()
var instance = Object.create(sharedPrototype);
ready
Crockford Create
var instance = crockfordCreate(sharedPrototype);
ready
Jorge Create
var instance = jorgeCreate(sharedPrototype);
ready
Constructor
var instance = new Constructor;
ready
SafeConstructor, using new
var instance = new SafeConstructor();
ready
SafeConstructor, without new
var instance = SafeConstructor();
ready
SafeConstructor, using new, without ()
var instance = new SafeConstructor;
ready

Revisions

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