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

thread-blocker

v1.0.1

Published

A utility package to block the Node.js main thread for testing purposes

Readme

Thread Blocker

A lightweight npm package to block the Node.js main thread for testing purposes. This is useful for testing timeout behaviors, event loop delays, and other scenarios where you need to simulate a busy main thread.

Installation

npm install thread-blocker

Features

  • 🚀 Block the main thread for a specified duration
  • ⚡ Both async and synchronous blocking methods
  • 🎯 CPU-intensive or low-intensity blocking options
  • 🔄 Repeat blocking patterns
  • 📊 Get information about blocking capabilities
  • 🧪 Perfect for testing and development

Usage

Basic Blocking

const { blockMainThread } = require('thread-blocker');

// Async blocking
async function testBlocking() {
  console.log('Starting block...');
  await blockMainThread(5000); // Block for 5 seconds
  console.log('Block complete!');
}

testBlocking();

Synchronous Blocking

const { blockMainThreadSync } = require('thread-blocker');

console.log('Blocking synchronously...');
blockMainThreadSync(3000); // Block for 3 seconds
console.log('Done!');

Advanced Options

const { block } = require('thread-blocker');

async function advancedBlocking() {
  await block({
    duration: 2000,
    intensive: true,
    callback: () => {
      console.log('Blocking callback executed');
    }
  });
}

advancedBlocking();

Repeated Blocking

const { blockRepeatedly } = require('thread-blocker');

async function repeatedBlocking() {
  // Block 5 times, each for 1 second, with 500ms interval between blocks
  await blockRepeatedly(1000, 5, 500);
  console.log('All blocks complete!');
}

repeatedBlocking();

Get Blocking Info

const { getBlockingInfo } = require('thread-blocker');

const info = getBlockingInfo();
console.log(info);
// Output:
// {
//   supported: true,
//   method: 'CPU-intensive blocking',
//   capabilities: [...]
// }

API Reference

blockMainThread(duration, intensive)

Blocks the main thread for the specified duration.

  • duration (number): Time to block in milliseconds
  • intensive (boolean, optional): Use CPU-intensive operations (default: true)
  • Returns: Promise

blockMainThreadSync(duration, intensive)

Synchronously blocks the main thread. ⚠️ Warning: This will block all operations.

  • duration (number): Time to block in milliseconds
  • intensive (boolean, optional): Use CPU-intensive operations (default: true)
  • Returns: void

block(options)

Advanced blocking with options.

  • options (BlockingOptions):
    • duration (number): Time to block in milliseconds
    • intensive (boolean, optional): Use CPU-intensive operations (default: true)
    • callback (function, optional): Function to call after blocking
  • Returns: Promise

blockRepeatedly(duration, count, interval)

Performs multiple blocks with optional intervals.

  • duration (number): Time for each block in milliseconds
  • count (number): Number of blocks to perform
  • interval (number, optional): Interval between blocks in milliseconds (default: 0)
  • Returns: Promise

getBlockingInfo()

Returns information about blocking capabilities.

  • Returns: Object with blocking info

Use Cases

  • ✅ Testing timeout handlers
  • ✅ Simulating slow operations
  • ✅ Testing event loop behavior
  • ✅ Performance testing
  • ✅ Stress testing applications
  • ✅ Testing UI responsiveness
  • ✅ Debugging event loop issues

Performance Notes

  • CPU-intensive mode (default): Uses Math.sqrt() in a loop to consume CPU cycles
  • Low-intensity mode: Uses a busy-wait loop with minimal CPU overhead

TypeScript Support

This package includes TypeScript definitions for full type support.

import { blockMainThread, BlockingOptions } from 'thread-blocker';

async function test() {
  await blockMainThread(5000, true);
}

⚠️ Warning

Do not use in production! This package is designed for testing and development purposes only. Using this in production code will severely impact your application's performance and responsiveness.

License

MIT

Contributing

Contributions are welcome! Please feel free to submit pull requests or open issues.