build(deps-dev): bump @types/node
[poolifier.git] / examples / javascript / fixedExample.cjs
CommitLineData
670ede1c 1'use strict'
6961ca9a
JB
2const {
3 FixedThreadPool,
4 PoolEvents,
5 availableParallelism
6} = require('poolifier')
7
d35e5717 8const pool = new FixedThreadPool(availableParallelism(), './yourWorker.cjs', {
041dc05b 9 errorHandler: e => console.error(e),
53795b86 10 onlineHandler: () => console.info('worker is online')
cf9aa6c3 11})
2431bdb4 12let poolReady = 0
6961ca9a 13let poolBusy = 0
8538ea4c
JB
14pool.emitter?.on(PoolEvents.ready, () => poolReady++)
15pool.emitter?.on(PoolEvents.busy, () => poolBusy++)
3e460d6d 16
6961ca9a 17let resolved = 0
15e5141f 18const start = performance.now()
bf962cba 19const iterations = 1000
292ad316 20for (let i = 1; i <= iterations; i++) {
583a27ce 21 pool
8923de44 22 .execute()
7a6a0a96 23 .then(() => {
583a27ce
JB
24 resolved++
25 if (resolved === iterations) {
8538ea4c
JB
26 console.info(
27 `Time taken is ${(performance.now() - start).toFixed(2)}ms`
28 )
1c132fec 29 console.info(`The pool was ready for ${poolReady} times`)
8538ea4c
JB
30 console.info(`The pool was busy for ${poolBusy} times`)
31 return pool.destroy()
583a27ce 32 }
fefd3cef 33 return undefined
583a27ce 34 })
041dc05b 35 .catch(err => console.error(err))
6dc67cda 36}