-
Notifications
You must be signed in to change notification settings - Fork 0
/
benchmark.js
40 lines (29 loc) · 904 Bytes
/
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
import { createThreadPool, spawn } from './spawn.js'
const NUM_RUNS = 1000
console.log("BENCHMARKING")
const nums = Array(NUM_RUNS).fill(1).map((x, i) => x + i)
// ==============================================
console.log("STARTING WITHOUT WORKERS")
const timer = stopwatch()
const vals = nums.map(expensive)
console.log("FINISHED AFTER", timer.stop, 's')
// ==============================================
console.log("STARTING WITH WORKERS")
timer.reset()
const threadPool = createThreadPool('thread.js')
Promise.all(nums.map(num => spawn(threadPool.worker, 'expensive', num)))
.then(vals => {
console.log("FINISHED AFTER", timer.stop, 's')
})
// ============ STOPWATCH UTIL =================
function stopwatch() {
let now = performance.now()
return {
reset() {
now = performance.now()
},
get stop() {
return (performance.now() - now) / 1000
}
}
}