@jsenv/workers

1.2.0 • Public • Published

jsenv workers npm package github main codecov coverage

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,
})

Readme

Keywords

none

Package Sidebar

Install

npm i @jsenv/workers

Weekly Downloads

1

Version

1.2.0

License

MIT

Unpacked Size

24.9 kB

Total Files

12

Last publish

Collaborators

  • jsenv-admin
  • dmail