ThreadPool
Quickly run CPU-intensive tasks in parallel across all JavaScript environments with a unified, type-safe API
Key Features
ThreadPool makes concurrent programming simple and consistent across all JavaScript runtimes and environments
Cross-Environment Compatible
One unified API that works across Node.js, Deno, Bun, and browser environments with consistent behavior and optimal performance in each runtime.
TypeScript First
Built in TypeScript with full type safety. Get autocompletion, parameter validation, and type checking for your worker functions out of the box.
Promise-Based API
Familiar Promise-like interface (
then, catch, finally, allSettled, all, any, race) for handling concurrent task execution and results.Simple to Use
ThreadPool provides a unified API across different environments
- Traditional Node Workers
- Server Worker Function
- Web Worker Function
ThreadPool provides a simplified API for working with Node.js Worker Threads. Create worker pools, add tasks with custom data, and handle results through a Promise-like interface.
import { WorkerPool } from '@renderdev/threadpool/node'
// Create a pool and import the worker script
const filename = new URL('./some-node-worker.ts', import.meta.url)
const pool = new WorkerPool(filename)
// Listen for when all the tasks are completed
pool.allSettled(threads => console.log(`All ${threads.length} tasks done!`))
// Create tasks by passing `workerData` to the worker
pool.addTask({ action: 'fibonacci', number: 42 })
// ...some more tasks
pool.addTask({ action: 'tribonacci', number: 32 })
Import any module and convert its exported functions into worker threads instantly. Maintain full TypeScript type checking while running functions in separate threads.
import { FunctionPool, importTaskWorker } from '@renderdev/threadpool/function'
// Import the types. Optional, but this gives us type checking
import type * as mathType from './math.ts'
// Convert any module into a worker function (with type checking)
const filename = new URL('./math.ts', import.meta.url)
const { fibonacci, tribonacci } = await importTaskWorker<typeof mathType>(filename)
// Create a pool
const pool = new FunctionPool()
// Listen for when all the tasks are completed
pool.allSettled(threads => console.log(`All ${threads.length} tasks completed`))
// Create tasks by converting the imported functions into threads that act as tasks
pool.addTask(fibonacci('42')) // Uh-oh! TS: fibonacci expects a number
// ...some more tasks
pool.addTask(tribonacci(32))
The same API works in browsers too! Create web workers from your functions with the same interface, making concurrent browser programming much simpler.
import { WebFunctionPool, importTaskWebWorker } from '@renderdev/threadpool/web'
// Import the types. Optional, but this gives us type checking
import type * as mathType from './math.ts'
// Convert any module into a worker function (with type checking)
const filename = new URL('./math.ts', import.meta.url)
const { fibonacci, tribonacci } = await importTaskWebWorker<typeof mathType>(filename)
// Create a pool
const pool = new WebFunctionPool()
// Listen for when all the tasks are completed
pool.allSettled(threads => console.log(`All ${threads.length} tasks completed`))
// Create tasks by converting the imported functions into threads that act as tasks
pool.addTask(fibonacci('42')) // Uh-oh! TS: fibonacci expects a number
// ...some more tasks
pool.addTask(tribonacci(32))
Powerful API, Simple Interface
- Customizable worker scripts for advanced use cases
- Configurations for pool size, system resource thresholds, and ping intervals
- Seamless integration with existing JavaScript and TypeScript modules
- Persistent workers with stateful interactions
- Shared Workers support in browser environments
Multi-Threading Made Simple
Streamline your development and enhance your JavaScript performance.