// pools
const externalPool = new Pool({ max: size })
-const fixedPool = new FixedThreadPool(size, './yourWorker.js', { maxTasks: 10000 })
-const dynamicPool = new DynamicThreadPool(size / 2, size * 3, './yourWorker.js', { maxTasks: 10000 })
+const fixedPool = new FixedThreadPool(size, './yourWorker.js', {
+ maxTasks: 10000
+})
+const dynamicPool = new DynamicThreadPool(
+ size / 2,
+ size * 3,
+ './yourWorker.js',
+ { maxTasks: 10000 }
+)
// data
const workerData = { proof: 'ok' }
for (let i = 0; i <= tasks; i++) {
fixedPool.execute(workerData).then(res => {
executions++
- if (executions === tasks) console.log(`Fixed pool take ${Date.now() - time} to work on ${executions} tasks`)
+ if (executions === tasks) {
+ console.log(
+ `Fixed pool take ${Date.now() - time} to work on ${executions} tasks`
+ )
+ }
})
}
}
for (let i = 0; i <= tasks; i++) {
dynamicPool.execute(workerData).then(res => {
executions++
- if (executions === tasks) console.log(`Dynamic pool take ${Date.now() - time} to work on ${executions} tasks`)
+ if (executions === tasks) {
+ console.log(
+ `Dynamic pool take ${Date.now() -
+ time} to work on ${executions} tasks`
+ )
+ }
})
}
}
const time = Date.now()
for (let i = 0; i <= tasks; i++) {
new Promise((resolve, reject) => {
- externalPool.acquire('./externalWorker.js', { workerData: workerData }, (err, worker) => {
- if (err) {
- return reject(err)
+ externalPool.acquire(
+ './externalWorker.js',
+ { workerData: workerData },
+ (err, worker) => {
+ if (err) {
+ return reject(err)
+ }
+ worker.on('error', reject)
+ worker.on('message', res => {
+ executions++
+ resolve(res)
+ })
}
- worker.on('error', reject)
- worker.on('message', res => {
- executions++
- resolve(res)
- })
- })
+ )
}).then(res => {
- if (tasks === executions) console.log(`External pool take ${Date.now() - time} to work on ${executions} tasks`)
+ if (tasks === executions) {
+ console.log(
+ `External pool take ${Date.now() -
+ time} to work on ${executions} tasks`
+ )
+ }
})
}
}