build(deps-dev): bump @types/node
[poolifier.git] / examples / javascript / fixedExample.js
1 'use strict'
2 const {
3 FixedThreadPool,
4 PoolEvents,
5 availableParallelism
6 } = require('poolifier')
7
8 const pool = new FixedThreadPool(availableParallelism(), './yourWorker.js', {
9 errorHandler: (e) => console.error(e),
10 onlineHandler: () => console.info('worker is online')
11 })
12 let poolReady = 0
13 let poolBusy = 0
14 pool.emitter.on(PoolEvents.ready, () => poolReady++)
15 pool.emitter.on(PoolEvents.busy, () => poolBusy++)
16
17 let resolved = 0
18 const start = performance.now()
19 const iterations = 1000
20 for (let i = 1; i <= iterations; i++) {
21 pool
22 .execute()
23 .then(() => {
24 resolved++
25 if (resolved === iterations) {
26 console.info('Time taken is ' + (performance.now() - start))
27 console.info('The pool was ready for ' + poolReady + ' times')
28 return console.info('The pool was busy for ' + poolBusy + ' times')
29 }
30 return null
31 })
32 .catch((err) => console.error(err))
33 }