npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

spawn-workers

v0.1.5

Published

A high-performance worker pool library for Node.js that spawns worker processes to handle tasks in parallel

Readme

spawn-workers

A high-performance worker pool library for Node.js that spawns worker processes to handle tasks in parallel.

Can be used for load testing, processing large datasets, running background jobs, or any task that can benefit from concurrent execution.

Features

  • Parallel Processing: Spawn multiple worker processes to handle tasks concurrently
  • Type-Safe: Full TypeScript support with generic custom status tracking
  • Configurable: Flexible configuration for batch sizes, concurrency limits, and more
  • Error Handling: Comprehensive error handling with custom error handlers
  • Progress Tracking: Real-time status updates and logging
  • Resource Management: Automatic cleanup and graceful shutdown

Installation

npm install spawn-workers
# or
bun install spawn-workers

Quick Start

1. Create a Worker

Create a worker file (worker.ts) that defines how to process each task:

import { runInWorker } from "spawn-workers";

runInWorker({
  handler: async ({ message }) => {

    // Process your task here
    const data = JSON.parse(message);
    
    // Simulate work
    await new Promise(resolve => setTimeout(resolve, 100));

  },
});

2. Spawn Workers

Create a main file (spawn.ts) to spawn and manage workers:

import { spawnWorkers } from "spawn-workers";
import path from "node:path";

const dirName = path.dirname(new URL(import.meta.url).pathname);

await spawnWorkers({
  workerFilePath: path.resolve(dirName, "./worker.ts"),
  dataFilePath: path.resolve(dirName, "./data.txt"),
  processCount: 2,
  batchSize: 2,
  maxConcurrency: 2,
  maxPendingJobs: 100,
  tickDuration: 500,
  logFilePath: "worker.log",
  onStatusUpdate: (statuses) => {
    console.clear();
    console.table(
      statuses.map((status, index) => ({
        started: status.started,
        completed: status.completed,
        failed: status.failed,
        pending: status.pending,
      }))
    );
  },
  onError: (error, worker) => {
    console.error(`Worker ${worker.index} error:`, error.message);
  },
  onComplete: () => {
    console.log("All tasks completed!");
  },
});

3. Prepare Data

Create a data file (data.txt) with one task per line:

{"id": 1, "name": "Task 1"}
{"id": 2, "name": "Task 2"}
{"id": 3, "name": "Task 3"}

Example Usage

Run the included example:

# Start the test server
npm run server

# In another terminal, run the worker pool
npm run spawn

# Or run both with a single command
npm run example

API Reference

runInWorker(config)

Configures a worker process to handle tasks.

Parameters:

  • handler: Function to process each task
  • customStatus: Initial custom status object

spawnWorkers(config)

Spawns worker processes to handle a pool of tasks.

Configuration:

  • workerFilePath: Path to the worker script
  • dataFilePath: Path to the data file
  • processCount: Number of worker processes
  • batchSize: Tasks sent per batch
  • maxConcurrency: Max concurrent tasks per worker
  • maxPendingJobs: Max pending jobs per worker
  • tickDuration: Interval that distributes workload to workers
  • onStatusUpdate: Status update callback
  • onError: Error handler callback
  • onComplete: Completion callback

Utilities

The library also exports a simple throttle function:

import { throttle } from "spawn-workers";


// Throttle function calls
const throttledUpdate = throttle(updateUI, 100);

License

MIT