Skip to content

Instantly share code, notes, and snippets.

@maxbeatty
Forked from bendc/functional-inheritance.js
Last active August 29, 2015 14:12
Show Gist options
  • Save maxbeatty/d0125f48188ceabf5be9 to your computer and use it in GitHub Desktop.
Save maxbeatty/d0125f48188ceabf5be9 to your computer and use it in GitHub Desktop.
Constructor comparisons in performance and memory usage
function car() {
return {
start: function() {
return "Engine on."
},
accelerate: function() {
return "Let's go!"
}
}
}
module.exports = function roadster(brand) {
var brand = brand
var self = car()
self.setBrand = function(name) {
brand = name
}
self.getBrand = function() {
return brand
}
return self
}
// benchmark implementation performance
assert = require('assert')
Benchmark = require('benchmark')
bdc = require('./bdc')
obj = require('./new')
suite = new Benchmark.Suite
suite
.add('bdc', function() {
car1 = bdc("Porsche")
assert.equal(car1.start(), 'Engine on.')
assert.equal(car1.getBrand(), "Porsche")
car1.setBrand("Jaguar")
assert.equal(car1.getBrand(), "Jaguar")
})
.add('new', function() {
car2 = new obj("Porsche")
assert.equal(car2.start(), 'Engine on.')
assert.equal(car2.getBrand(), "Porsche")
car2.setBrand("Jaguar")
assert.equal(car2.getBrand(), "Jaguar")
})
.on('cycle', function(event) {
console.log(String(event.target))
})
.on('complete', function() {
console.log('Fastest is ' + this.filter('fastest').pluck('name'))
})
.run()
// measure memory usage for implementations
// NOTE: run with --expose-gc flag (e.g. `node --expose-gc memoryUsage.js`)
bdc = require('./bdc')
obj = require('./new')
function measure(n, constr) {
for(var i = 0; i < n; i++) {
constr()
}
return process.memoryUsage().heapUsed
}
steps = [10, 100, 1000, 10000]
for(var j = 0; j < steps.length; j++) {
var count = steps[j]
gc() // garbage collect
bdcMem = measure(count, function() { bdc("Porsche") })
gc() // garbage collect
newMem = measure(count, function() { new obj("Porsche") })
gc() // garbage collect
if (bdcMem < newMem) {
fasterName = 'bdc'
diff = newMem - bdcMem
} else {
fasterName = 'new'
diff = bdcMem - newMem
}
console.log("At " + count + ", " + fasterName + " used " + diff + " less bytes")
}
Car = function() {}
Car.prototype.start = function() {
return "Engine on."
}
Car.prototype.accelerate = function() {
return "Let's go!"
}
Roadster = function(brand) {
this.brand = brand
Car.call(this)
}
Roadster.prototype = Object.create(Car.prototype)
Roadster.prototype.constructor = Roadster
Roadster.prototype.setBrand = function(name) {
this.brand = name
}
Roadster.prototype.getBrand = function() {
return this.brand
}
module.exports = Roadster
// test that two implementations are equal in functionality
assert = require('assert')
/***********/
bdc = require('./bdc')
car1 = bdc("Porsche")
assert.equal(car1.start(), 'Engine on.')
assert.equal(car1.getBrand(), "Porsche")
car1.setBrand("Jaguar")
assert.equal(car1.getBrand(), "Jaguar")
/***********/
obj = require('./new')
car2 = new obj("Porsche")
assert.equal(car2.start(), 'Engine on.')
assert.equal(car2.getBrand(), "Porsche")
car2.setBrand("Jaguar")
assert.equal(car2.getBrand(), "Jaguar")
@maxbeatty
Copy link
Author

Benchmark results:

bdc x 1,734,638 ops/sec ±4.95% (80 runs sampled)
new x 10,171,972 ops/sec ±10.05% (62 runs sampled)
Fastest is new

Memory usage results:

At 10, new used 5304 less bytes
At 100, new used 69232 less bytes
At 1000, new used 396392 less bytes
At 10000, new used 2264392 less bytes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment