classical vsproto vs functional

Benchmark created by devu on


Setup

//Class inheritance model
    var Class = function() {};
    var p = Class.prototype;
    Function.prototype.extend = function(C) {
      Class.prototype = C.prototype;
      this.prototype = new Class();
      this.prototype.constructor = this;
      return this.prototype
    };

Test runner

Ready to run.

Testing in
TestOps/sec
Classical
function CPerson(name) {
  this.name = name;
  this.introduce = function() {
    return 'Hi, ' + this.name + ' here!';
  }
}

CEmploy.extend(CPerson);

function CEmploy(name) {
  CPerson.call(this, name);

  this.introduce = function() {
    return 'Employ Hi, ' + this.name + ' here!'
  }
}
for (var i = 0; i < 100; i++) {
  var Cbart = new CEmploy('Bart');
  Cbart.introduce();
}
ready
Classical with Proto
p = PPerson.extend(Class);

function PPerson(name) {
  this.name = name;
}
p.introduce = function() {
  return 'Hi, ' + this.name + ' here!';
}

p = PEmploy.extend(PPerson);

function PEmploy(name) {
  PPerson.call(this, name);
}

p.introduce = function() {
  return 'Hi, ' + this.name + ' here!';
}

for (var i = 0; i < 100; i++) {
  var Pbart = new PEmploy('Bart');
  Pbart.introduce();
}
ready
Functioinal
function FPerson(name) {
  var p = {
    name: name,
    introduce: function() {
      return 'Hi, ' + this.name + ' here!';
    }
  };

  return p
}

function FEmploy(name) {
  var p = FPerson(name);

  p.introduce = function() {
    return 'Employ Hi, ' + p.name + ' here!'
  }
  return p;
}

for (var i = 0; i < 100; i++) {
  var Fbart = FEmploy('Bart');
  Fbart.introduce();
}
ready
Functional II
function FIIPerson(name) {
  return {
    name: name,
    introduce: function() {
      return 'Hi, ' + this.name + ' here!';
    }
  };
}

function FIIEmploy(name) {
  var p = FIIPerson(name);

  p.introduce = function() {
    return 'Employ Hi, ' + p.name + ' here!'
  }
  return p;
}

for (var i = 0; i < 100; i++) {
  var FIIbart = FIIEmploy('Bart');
  FIIbart.introduce();
}
ready

Revisions

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