Skip to content

Instantly share code, notes, and snippets.

@sminutoli
Created July 21, 2017 15:33
Show Gist options
  • Save sminutoli/1c60b67e5b7c2b0b5bce8417e8834f37 to your computer and use it in GitHub Desktop.
Save sminutoli/1c60b67e5b7c2b0b5bce8417e8834f37 to your computer and use it in GitHub Desktop.
const Cow = {
airInLungs: 0,
getAirInLungs() {
return this.airInLungs
},
breathe () {
this.airInLungs = this.lungCapacity
},
moo () {
let output = "m"
let air = this.getAirInLungs()
while (air --> 0) { // The 'goes to' operator
output += "o"
}
this.airInLungs = air
return output
}
}
const CowFactory = lungCapacity => ({
__proto__: Cow,
lungCapacity
});
const herd = []
for (let i = 0; i < 30000; i++) {
const cow = CowFactory(i);
cow.index = i
herd.push(cow)
}
console.log(process.memoryUsage())
const start = Date.now()
herd.map(cow => {
cow.breathe()
cow.moo()
})
console.log(`Finished mooing in ${(Date.now() - start) / 1000} seconds`)
/*
My results…
➜ class-vs-closures node classes.js
{ rss: 25632768, heapTotal: 11571200, heapUsed: 6076680 }
Finished mooing in 8.192 seconds
➜ class-vs-closures node closures.js
{ rss: 38068224, heapTotal: 29396992, heapUsed: 15660968 }
Finished mooing in 14.56 seconds
➜ class-vs-closures node oloo.js
{ rss: 27602944, heapTotal: 16814080, heapUsed: 6630416 }
Finished mooing in 8.212 seconds
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment