Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zefhemel/319415 to your computer and use it in GitHub Desktop.
Save zefhemel/319415 to your computer and use it in GitHub Desktop.
const LOOP_COUNT = 1000 * 1000;
var startTime = 0;
function benchStart() {
startTime = new Date().getTime();
}
function printTime() {
console.log((new Date().getTime())- startTime);
}
////// Pseudo classical
function LivingBeing(age) {
this.age = age;
}
LivingBeing.prototype.getAge = function() {
return this.age;
};
function Person(name, age) {
this.name = name;
this.age = age;
}
Person.prototype = new LivingBeing();
Person.prototype.getNameAndAge = function() {
return "Name: " + this.name + " and age: " + this.getAge();
};
/////// Testing it
console.log("Pseudo classical inheritance...");
for(var c = 0; c < 10; c++) {
benchStart();
var p = new Person("Person " + i, i+1);
for(var i = 0; i < LOOP_COUNT; i++) {
p.getNameAndAge();
}
printTime();
}
/////// Functional
function livingBeing(age) {
return {
getAge: function() {
return age;
}
};
}
function person(name, age) {
var that = livingBeing(age);
that.getNameAndAge = function() {
return "Name: " + name + " and age: " + that.getAge();
};
return that;
}
/////// Testing it
console.log("And now functional inheritance...");
for(var c = 0; c < 10; c++) {
benchStart();
var p = person("Person " + i, i+1);
for(var i = 0; i < LOOP_COUNT; i++) {
p.getNameAndAge();
}
printTime();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment