-
-
Save maxbeatty/d0125f48188ceabf5be9 to your computer and use it in GitHub Desktop.
Constructor comparisons in performance and memory usage
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
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 | |
} |
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
// 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() |
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
// 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") | |
} |
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
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 |
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
// 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") |
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
Test
Benchmark
Same library as jsperf.
Memory Usage
This is an admittedly naive approach to trying to measure memory usage by the two constructors.