Skip to content

Instantly share code, notes, and snippets.

@zefhemel
Created March 1, 2010 18:52
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/318662 to your computer and use it in GitHub Desktop.
Save zefhemel/318662 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 Person(name, age) {
this.name = name;
this.age = age;
}
Person.prototype.getNameAndAge = function() {
return "Name: " + this.name + " and age: " + this.age;
};
/////// Testing it
console.log("Pseudo classical inheritance...");
for(var c = 0; c < 10; c++) {
benchStart();
var p;
for(var i = 0; i < LOOP_COUNT; i++) {
p = new Person("Person " + i, i+1);
}
printTime();
}
console.log("Method calling");
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 person(name, age) {
return {
getNameAndAge: function() {
return "Name: " + name + " and age: " + age;
}
};
}
/////// Testing it
console.log("And now functional inheritance...");
for(var c = 0; c < 10; c++) {
benchStart();
var p;
for(var i = 0; i < LOOP_COUNT; i++) {
p = person("Person " + i, i+1);
}
printTime();
}
console.log("Method calling...");
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