-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
benchmark.js
51 lines (45 loc) · 1.32 KB
/
benchmark.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
const assert = require("node:assert");
const Benchmark = require("benchmark");
const yargs = require("yargs/yargs");
const { hideBin } = require("yargs/helpers");
const { collatzSteps: collatzJs } = require("./js/collatz");
const { collatzSteps: collatzFfi } = require("./ffi/collatz");
const { collatzSteps: collatzWasm } = require("./wasm/collatz");
const argv = yargs(hideBin(process.argv))
.option("input", {
alias: "i",
describe: "Input number for the Collatz conjecture",
type: "number",
demandOption: true,
nargs: 1,
})
.check((argv) => {
if (!Number.isInteger(argv.input) || argv.input <= 0) {
throw new Error("Input must be a positive integer");
}
return true;
}).argv;
const { input } = argv;
const stepsJs = collatzJs(input);
const stepsFfi = collatzFfi(input);
assert.equal(stepsJs, stepsFfi);
const stepsWasm = collatzWasm(input);
assert.equal(stepsJs, stepsWasm);
const suite = new Benchmark.Suite();
suite
.add("JS", function () {
collatzJs(input);
})
.add("FFI", function () {
collatzFfi(input);
})
.add("WASM", function () {
collatzWasm(input);
})
.on("cycle", function (event) {
console.log(String(event.target));
})
.on("complete", function () {
console.log("Fastest is " + this.filter("fastest").map("name"));
})
.run({ async: true });