-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
44 lines (33 loc) · 923 Bytes
/
index.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
const rustAddon = require('./rust-addon')
const wasmPack = require('./wasm-pack/pkg')
const neon = require('./neon')
const napiRs = require('./napi-rs-addon')
const nodeBindgen = require('./node-bindgen-addon')
const NS_PER_SEC = 1e9
const modules = {
rustAddon,
wasmPack,
neon,
napiRs,
nodeBindgen
}
const pairs = Array(1000000).fill().map(() => [Math.random(), Math.random()])
function bench (func, jsTime) {
let totalTime = 0
for (const [a, b] of pairs) {
const start = process.hrtime()
func(a, b)
const diff = process.hrtime(start)
totalTime += diff[0] + (diff[1] / NS_PER_SEC)
}
return totalTime / jsTime
}
function add (a, b) {
return a + b
}
const jsTime = bench(add, 1)
const results = [{ module: 'js', relativeTime: 1 }]
for (const mod of Object.keys(modules)) {
results.push({ module: mod, relativeTime: bench(modules[mod].add, jsTime) })
}
console.table(results)