@jsenv/workers
v1.2.0
Published
Split CPU intensive code into worker threads
Downloads
23
Readme
jsenv workers
Split CPU intensive code into worker threads.
Example
worker.mjs:
import { parentPort } from "node:worker_threads"
parentPort.on("message", async ({ a, b }) => {
await new Promise((resolve) => setTimeout(resolve, 100))
const returnValue = a + b
parentPort.postMessage(returnValue)
})
main.mjs:
import { createWorkers } from "@jsenv/workers"
const workers = createWorkers(new URL("./worker.mjs", import.meta.url))
const value = await workers.addJob({ a: 1, b: 1 })
console.log(value) // 2
Static pool
By default the pool size is dynamic but it can be fixed to X workers.
import { createWorkers } from "@jsenv/workers"
const workers = createWorkers(new URL("./worker.mjs"), {
minWorkers: 10,
maxWorkers: 10,
})