forked from oven-sh/bun
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathadd3.bun.js
49 lines (42 loc) · 986 Bytes
/
add3.bun.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
import { run, bench, group, baseline } from "mitata";
import { dlopen, suffix } from "bun:ffi";
import { readdirSync } from "fs";
const {
symbols: {
add3: { native: add3 },
noop,
},
close,
} = dlopen(`./plus100.dylib`, {
add3: {
args: ["int32_t", "int32_t", "int32_t"],
returns: "int32_t",
},
noop: {
args: [],
},
});
const { add3: add3napi, noop: noopNapi } = require("./plus100-napi/index.js");
group("add3", () => {
bench("add3(1,2,3) ffi", () => {
add3(1, 2, 3);
});
bench("add3(1,2,3) napi", () => {
add3napi(1, 2, 3);
});
});
group("noop", () => {
bench("noop() ffi", () => {
noop();
});
bench("noop() napi", () => {
noopNapi();
});
});
// collect option collects benchmark returned values into array
// prevents gc and can help with jit optimizing out functions
await run({ collect: false, percentiles: true });
console.log("\n");
if (add3(1, 2, 3) !== 1 + 2 + 3) {
throw new Error("add3(1) !== 101");
}