2 const { spawn
, Thread
, Worker
} = require('threads')
3 // FINISH IMPORT LIBRARIES
4 const size
= Number(process
.env
.POOL_SIZE
)
5 const iterations
= Number(process
.env
.NUM_ITERATIONS
)
8 taskType
: process
.env
['TASK_TYPE']
11 // Threads.js is not really a pool so we need to write few additional code
13 async
function poolify () {
14 for (let i
= 0; i
< size
; i
++) {
15 const worker
= await
spawn(
16 new Worker('./workers/threadjs/function-to-bench-worker.js')
22 async
function run () {
25 for (let i
= 0; i
< iterations
; i
++) {
26 const worker
= workers
[i
% size
]
27 promises
.push(worker
.exposedFunction(data
))
29 await Promise
.all(promises
)