Skip to main content

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

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.
type TaskPoolOptions = {pingInterval?: numberpoolSize?: numberemitter: (event: string) => void}

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

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

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

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
See More Advanced Examples

Multi-Threading Made Simple

Streamline your development and enhance your JavaScript performance.