Created
March 2, 2010 10:22
-
-
Save zefhemel/319415 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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