Differents ways of inheritance

JavaScript performance comparison

Test case created by Josema and last updated

Preparation code

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

Preparation code output

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…
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 (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()
pending…
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()
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