Chapter 10: Optimizing Synchronous Functions samples

This commit is contained in:
Beth Griggs
2020-08-30 23:30:37 +01:00
parent ef82559a0e
commit 575ab40a00
5 changed files with 88 additions and 0 deletions
+23
View File
@@ -0,0 +1,23 @@
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);
}