Differents ways of inheritance (v10)

Revision 10 of this benchmark created on


Preparation HTML

<script src="https://raw.github.com/javascript/augment/master/lib/augment.js"></script>
<script src="https://raw.github.com/dotnetwise/Javascript-FastClass/master/FastClass.js"></script>

Test runner

Ready to run.

Testing in
TestOps/sec
Pseudo-classical inheritance (through prototyping):
function A() {
  this.pro1 = 'pro1 ';
  this.method1 = function() {
    return this.pro1
  }
}

function B() {
  this.pro2 = 'pro2 ';
  this.method2 = function() {
    return this.pro1 + this.pro2;
  };
}
B.prototype = new A();

function C() {
  this.pro3 = 'pro3 ';
  this.method3 = function() {
    return this.pro1 + this.pro2 + this.pro3 + this.method1() + this.method2();
  };
}
C.prototype = new B();

inst = new C()
inst.method3()
ready
Function application or "constructor chaining"
function A() {
  this.pro1 = 'pro1 ';
  this.method1 = function() {
    return this.pro1
  }
}

function B() {
  this.pro2 = 'pro2 ';
  this.method2 = function() {
    return this.pro1 + this.pro2;
  };
  A.apply(this, arguments);
}

function C() {
  this.pro3 = 'pro3 ';
  this.method3 = function() {
    return this.pro1 + this.pro2 + this.pro3 + this.method1() + this.method2();
  };
  B.apply(this, arguments);
}

inst = new C()
inst.method3()
ready
Parasitic inheritance or Power Constructors
function A() {
  var A = {
    pro1: 'pro1 ',
    method1: function() {
      return this.pro1
    }
  };
  return A;
}

function B() {
  var obj = A();
  obj.pro2 = 'pro2 ';
  obj.method2 = function() {
    return this.pro1 + this.pro2;
  };
  return obj;
}

function C() {
  var obj = B();
  obj.pro3 = 'pro3 ';
  obj.method3 = function() {
    return this.pro1 + this.pro2 + this.pro3 + this.method1() + this.method2();
  };
  return obj;
}


inst = C();
inst.method3()
ready
ECMAScript 5th Ed. Object.create method:
var A = {
  pro1: 'pro1 ',
  method1: function() {
    return this.pro1
  }
};

var B = Object.create(A);
B.pro2 = 'pro2 ';
B.method2 = function() {
  return this.pro1 + this.pro2;
};

var C = Object.create(B);
C.pro3 = 'pro3 ';
C.method3 = function() {
  return this.pro1 + this.pro2 + this.pro3 + this.method1() + this.method2();
};

C.method3();
ready
Parasitic inheritance or Power Constructors (With for)
function A() {
  var A = {
    pro1: 'pro1 ',
    method1: function() {
      return this.pro1
    }
  };
  return A;
}

function B() {
  var obj = {};
  obj.pro2 = 'pro2 ';
  obj.method2 = function() {
    return this.pro1 + this.pro2;
  };
  var a = A();
  for (i in a)
    obj[i] = a[i];
  return obj;
}

function C() {
  var obj = {};
  obj.pro3 = 'pro3 ';
  obj.method3 = function() {
    return this.pro1 + this.pro2 + this.pro3 + this.method1() + this.method2();
  };
  var b = B();
  for (i in b)
    obj[i] = b[i];
  return obj;
}


inst = C();
inst.method3()
ready
augment.js
var A = Object.augment(function() {
  this.constructor = function() {}
  this.pro1 = 'pro1 ';
  this.method1 = function() {
    return this.pro1
  }
});

var B = A.augment(function(base) {
  this.constructor = function() {}
  this.pro2 = 'pro2 ';
  this.method2 = function() {
    return this.pro1 + this.pro2;
  };
});

var C = B.augment(function(base) {
  this.constructor = function() {}
  this.pro3 = 'pro3 ';
  this.method3 = function() {
    return this.pro1 + this.pro2 + this.pro3 + this.method1() + this.method2();
  };
});

inst = new C();
inst.method3()
ready

Revisions

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