Jul 21, 2017 · 1 min read
Hi Richard, thanks for this articles.
What about OLOO? Objects linked without classes at all.
'use strict'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
