Different ways of native inheritance

JavaScript performance comparison

Revision 2 of this test case created by Josema

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in unknown unknown
Test Ops/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()
pending…
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()
pending…
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()
pending…
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()
pending…
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();
 
pending…
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()
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:

0 comments

Add a comment