Object.create vs Crockford vs Constructor

JavaScript performance comparison

Revision 32 of this test case created

Info

Wondering how much of a difference hoisting f out of crockfordCreate will make.

Preparation code

<script>
  var sharedPrototype = {
    one: function() {
      return 1;
    },
    two: function() {
      return 2;
    },
    three: function() {
      return 3;
    }
  };

  var crockfordCreate = function(o) {
      function F() {}
      F.prototype = o;
      return new F();
      };

  var Constructor = function() {
      this.one = function() {
        return 1;
      };
      this.two = function() {
        return 2;
      };
      this.three = function() {
        return 3;
      }

      return this;
      };
  Constructor.prototype = sharedPrototype;
</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()
var i = 100;
while (i--) Object.create(sharedPrototype);
pending…
Crockford Create
var i = 100;
while (i--) crockfordCreate(sharedPrototype);
pending…
Constructor
var i = 100;
while (i--) new Constructor;
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:

2 comments

A. Matías Quezada commented :

The "constructor" function you use is the old crockford system. Try to add the object's method by prototype instead of create 3 functions and inject them on the object on each instanciation.

http://jsperf.com/create-or-new/2

A. Matías Quezada commented :

PD: See next revision > http://jsperf.com/object-create-vs-crockford-vs-jorge-vs-constructor/33

Add a comment