Different ways of native inheritance (v2)

Revision 2 of this benchmark created by Josema on


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
Copying prototype iterating the Function.prototype
function A(){}
A.prototype.pro1 = 'pro1 ';
A.prototype.method1 = function(){ return this.pro1 }

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

for (i in A.prototype)
    B.prototype[i] = A.prototype[i]

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

for (i in B.prototype)
    C.prototype[i] = B.prototype[i]


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 (Iterating)
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

Revisions

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