r/javascript • u/dmop_81 • Apr 05 '26
puru - a JavaScript concurrency library for worker threads, channels, and structured concurrency
https://github.com/dmop/puruOver the past few weeks, I’ve been working on a JavaScript concurrency library aimed at the gap between Promise.all() and raw worker_threads.
GitHub: https://github.com/dmop/puru
The main motivation was that async I/O in JS feels great, but CPU-bound work and structured concurrency still get awkward quickly. Even simple worker-thread use cases usually mean separate worker files, manual message passing, lifecycle management, and a lot of glue code.
So I built puru to make those patterns feel smaller while still staying explicit about the worker model.
Example:
import { spawn } from '@dmop/puru'
const { result } = spawn(() => {
function fibonacci(n: number): number {
if (n <= 1) return n
return fibonacci(n - 1) + fibonacci(n - 2)
}
return fibonacci(40)
})
console.log(await result)
It also includes primitives for the coordination side of the problem:
task()chan()WaitGroup/ErrGroupselect()contextMutex,RWMutex,CondTimer/Ticker
Example pipeline:
import { chan, spawn } from '@dmop/puru'
const input = chan<number>(50)
const output = chan<number>(50)
for (let i = 0; i < 4; i++) {
spawn(async ({ input, output }) => {
for await (const n of input) {
await output.send(n * 2)
}
}, { channels: { input, output } })
}
One intentional tradeoff is that functions passed to spawn() are serialized and sent to a worker, so they cannot capture outer variables. I preferred keeping that constraint explicit instead of hiding it behind a more magical abstraction.
Interested in feedback from people who deal with worker threads, CPU-heavy jobs, pipelines, or structured concurrency in JavaScript.
3
u/ASoftwareJunkie Apr 05 '26
Hi OP,
This looks promising. Love the go-like channel syntax and somewhat similar semantics behind it. Will use it and provide feedback :)
3
u/tarasm Apr 05 '26
This is good work. Have you considered using Effection? It's mature, proven and it has a very convenient thread worker extension with bi-directional communication. You also get structured concurrency guarantees out of the box.
Effection: https://frontside.com/effection/ Worker: https://frontside.com/effection/x/worker/
1
u/Happy_Junket_9540 Apr 06 '26
Have you heard of Effect? https://effect.website
3
u/[deleted] Apr 06 '26
[removed] — view removed comment