Created
March 1, 2010 18:52
-
-
Save zefhemel/318662 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 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