Files
Node.js-14-Cookbook/Chapter10/optimize-sync/benchmark.js
T
2020-08-30 23:30:37 +01:00

24 lines
552 B
JavaScript

const benchmark = require("benchmark");
const slow = require("./slow");
const loop = require("./loop");
const suite = new benchmark.Suite();
const maxNumber = 100; // number to pass through to sumOfSquares()
suite.add("slow", function () {
slow(maxNumber);
});
suite.add("loop", function () {
loop(maxNumber);
});
suite.on("complete", printResults);
suite.run();
function printResults() {
this.forEach((benchmark) => {
console.log(benchmark.toString());
});
console.log("Fastest implementation is", this.filter("fastest")[0].name);
}