-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathfixedExample.js
More file actions
38 lines (36 loc) · 999 Bytes
/
fixedExample.js
File metadata and controls
38 lines (36 loc) · 999 Bytes
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
import {
availableParallelism,
FixedThreadPool,
PoolEvents,
} from 'jsr:@poolifier/poolifier-web-worker@^0.5.0' // x-release-please-version
const pool = new FixedThreadPool(
availableParallelism(),
new URL('./yourWorker.js', import.meta.url),
{
errorEventHandler: (e) => {
console.error(e)
},
},
)
let poolReady = 0
let poolBusy = 0
pool.eventTarget?.addEventListener(PoolEvents.ready, () => poolReady++)
pool.eventTarget?.addEventListener(PoolEvents.busy, () => poolBusy++)
let resolved = 0
const start = performance.now()
const iterations = 1000
for (let i = 1; i <= iterations; i++) {
try {
await pool.execute()
resolved++
if (resolved === iterations) {
console.info(`Time taken is ${(performance.now() - start).toFixed(2)}ms`)
console.info(`The pool was ready for ${poolReady} times`)
console.info(`The pool was busy for ${poolBusy} times`)
await pool.destroy()
break
}
} catch (err) {
console.error(err)
}
}