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

superthreads

v2.0.0

Published

High-performance multithreading utilities for Node.js using worker_threads — simplified and optimized.

Downloads

11

Readme

⚡️ Superthreads — Effortless Multithreading for Node.js

🧵 High-performance, elegant multithreading utilities built on top of Node.js worker_threads. 🚀 Simplify concurrency, scale effortlessly, and run code in parallel like a pro.

npm license node build


✨ Features

  • 🧠 Thread — Run isolated functions in worker threads easily.
  • 🧩 ThreadPool — Manage multiple threads efficiently with queuing and priorities.
  • 🔁 ReusableThread — Keep a worker alive and reuse it for multiple tasks.
  • ⚙️ ThreadPoolReusable — Hybrid pool using reusable threads.
  • ⚙️ AutoScalingPoolReusable — Hybrid pool using reusable threads, dynamically scales workers up or down based on load with re-use logic.
  • 📈 AutoScalingPool — Dynamically scales workers up or down based on load.
  • 🚩 AbortToken — Graceful task cancellation support.
  • 🧰 Task — Promise-like task wrapper with additional metadata.

📦 Installation

npm install superthreads

or

yarn add superthreads

🧙‍♂️ Quick Start

🧵 Simple Thread

const { Thread } = require('superthreads');

(async () => {
  const result = await Thread.run((n) => n * 2, 21);
  console.log('Result:', result); // 42
})();

⚙️ ThreadPool Example

const { ThreadPool } = require('superthreads');
const path = require('node:path');

const pool = new ThreadPool(4, { imports: ['fs/promises'] });

// Optional: attach some event listeners for visibility
pool.on('taskStart', ({ taskId, workerId }) => console.log(`Task ${taskId} started (worker ${workerId})...`));
pool.on('taskEnd', ({ taskId, workerId }) => console.log(`Task ${taskId} finished (worker ${workerId}).`));
pool.on('error', (err, meta) => console.error('Error in the pool:', err, meta));
pool.on('workerLog', ({ workerId, msg }) => console.log(`WorkerLog ${workerId}:`, msg));
pool.on('taskMessage', ({ taskId, message }) => console.log('Message from worker (task):', taskId, message));

async function main() {
  console.log('Initial stats:', pool.stats());

  // 🧮 CPU-intensive task
  const r1 = await pool.run((n) => {
    let total = 0;
    for (let i = 0; i < n; i++) total += i;
    return total;
  }, 1e7);

  console.log('Result 1:', r1);

  // 📄 Async FS operation with context port
  const task = pool.run(async (data, context) => {
    const fsPromises = global.promises;
    const stats = await fsPromises.stat(data.file);

    if (context.port) {
      context.port.postMessage({ status: 'Mid progress' });
    }

    return stats.size;
  }, { file: path.join(__dirname, '..', 'package.json') }, { priority: 'high' });

  console.log('Result 2:', await task);
  await pool.shutdown();

  console.log('Final stats:', pool.stats());
}

main().catch(console.error);

🔁 Reusable Threads

Keep a thread alive and reuse it for multiple tasks:

const { ReusableThread } = require('superthreads');

(async () => {
  const thread = new ReusableThread();

  const result1 = await thread.run((n) => n * 2, 21);
  console.log('result1:', result1); // 42

  const result2 = await thread.run((s) => `Hello, ${s}!`, 'World');
  console.log('result2:', result2); // Hello, World!

  await thread.terminate();
})();

🧵 ThreadPoolReusable Example

Run multiple reusable threads efficiently:

const { ThreadPoolReusable } = require('superthreads');

const pool = new ThreadPoolReusable(4);

(async () => {
  const tasks = [
    pool.run((n) => n * 2, 10),
    pool.run((n) => n + 5, 37),
    pool.run((s) => s.toUpperCase(), 'superthreads')
  ];

  const results = await Promise.all(tasks);
  console.log('Results:', results); // [20, 42, "SUPERTHREADS"]

  await pool.terminate();
})();

🧠 AutoScalingPool Example

Automatically scale threads based on demand:

const { AutoScalingPool } = require('superthreads');

const autoPool = new AutoScalingPool({ min: 2, max: 8 });

(async () => {
  const results = await Promise.all(
    Array.from({ length: 10 }, (_, i) =>
      autoPool.run((x) => x ** 2, i)
    )
  );

  console.log('Squares:', results);
  await autoPool.shutdown();
})();

🧠 AutoScalingPoolReusable Example

'use strict';
const path = require('node:path');
const { AutoScalingPoolReusable } = require('superthreads');

const pool = new AutoScalingPoolReusable({
  min: 2,
  max: 6,
  checkInterval: 2000,
  idleTimeout: 5000,
  imports: ['fs/promises']
});

pool.on('scalingUp', ({ from, to }) =>
  console.log(`🆙 Scaling up: from ${from} → ${to}`)
);

pool.on('scalingDown', ({ threadId }) =>
  console.log(`⬇️ Scaling down: removing thread ${threadId}`)
);

pool.on('error', (err, meta) =>
  console.error('❌ Pool error:', err, meta)
);

pool.on('shutdown', () =>
  console.log('✅ Pool shutdown complete')
);

async function main() {
  console.log('📊 Initial stats:', pool.stats ? pool.stats() : 'N/A');

  const task1 = pool.run((n) => {
    let total = 0;
    for (let i = 0; i < n; i++) total += i;
    return total;
  }, 5e7);

  const task2 = pool.run(async (data) => {
    const fs = global.promises;
    const stats = await fs.stat(data.file);
    return { file: data.file, size: stats.size };
  }, { file: path.join(__dirname, '..', 'package.json') });

  const task3 = pool.run(async (ms) => {
    await new Promise(r => setTimeout(r, ms));
    return `Slept ${ms}ms`;
  }, 2000);

  const results = await Promise.all([task1, task2, task3]);
  console.log('📦 Results:', results);

  for (let i = 0; i < 10; i++) {
    pool.run(async (x) => {
      await new Promise(r => setTimeout(r, 500 + Math.random() * 1500));
      return `Processed item ${x}`;
    }, i).then(res => console.log('✅', res));
  }

  console.log('⏳ Waiting for scaling to occur...');
  await new Promise(r => setTimeout(r, 12000));

  console.log('📉 Stats before shutdown:', pool.stats ? pool.stats() : 'N/A');
  await pool.shutdown();
}

main().catch(err => console.error(err));

🚩 AbortToken Example

Cancel a task before it completes:

const { Thread, AbortToken } = require('superthreads');

(async () => {
  const controller = new AbortToken();
  const signal = controller.signal;

  const task = Thread.run(async (_, ctx) => {
    for (let i = 0; i < 5; i++) {
      if (ctx.signal?.aborted) throw new Error('Aborted!');
      await new Promise(r => setTimeout(r, 500));
    }
    return 'Done';
  }, null, { signal });

  setTimeout(() => controller.abort(), 1000); // Abort after 1s

  try {
    console.log(await task);
  } catch (err) {
    console.error('Task aborted:', err.message);
  }
})();

📊 Stats API

Every pool provides pool.stats() for quick insight:

console.log(pool.stats());
// {
//   totalWorkers: 4,
//   idleWorkers: 2,
//   activeWorkers: 2,
//   pendingTasks: 3
// }

🛠️ Requirements

  • Node.js >= 18.0.0
  • Works with both CommonJS (require) and ESM (import).

🔥 Why Superthreads?

  • Zero dependencies
  • Simple, readable API
  • Advanced thread reuse & pooling
  • Safe aborts, smart auto-scaling, and detailed stats
  • Built for real-world high-performance Node.js applications

⚖️ License

MIT © 2025 — Created with ❤️ by ZygoteCode


🚀 Made with Node.js, love, and way too much coffee.