Prototypal creation vs Functional creation (v10)

Revision 10 of this benchmark created on


Description

This test suite compares performances of pure “prototypal creation” and “functional creation”. Both patterns have been well explained by Douglas Crockford in his book: JavaScript, The Good Parts.

Setup

// Functional inheritance
    var object = function () {
      var that = {
        prop: 'prop',
        method: function () {
          return that.prop;
        }
      };
      return that;
    };
    
    
    
    // Prototypal inheritance
    var inherits = function(Child, Parent) {
      function F() { }
      F.prototype = Parent.prototype;
      Child.prototype = new F;
    };
    
    var Object = function () {};
    Object.prototype.prop = 'prop';
    Object.prototype.method = function () {
      return this.prop;
    };
    
    
    
    // Prototypal inheritance where all functions are bound to this
    var BObject = function () {
      this.method = this.method.bind(this);
    };
    BObject.prototype.prop = 'prop';
    BObject.prototype.method = function () {
      return this.prop;
    };
    
    
    var o = object();
    
    var O = new Object();
    
    var B = new BObject();

Test runner

Ready to run.

Testing in
TestOps/sec
object()
var _x = object();
ready
new Object()
var _y = new Object;
ready
obj.method()
var _xx = o.method();
 
ready
O.method()
var _yy = O.method();
 
ready
new BObject()
var _z = new BObject();
ready
BOb.method()
var _zz = B.method();
 
ready

Revisions

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