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 🙏

© 2025 – Pkg Stats / Ryan Hefner

yieldtomain

v1.0.3

Published

A utility function to yield to the main thread in JavaScript

Readme

yieldToMain

A utility function to yield execution to the main thread in JavaScript, preventing UI blocking. Uses scheduler.yield() when available, falling back to setTimeout.

The gist is this:

function yieldToMain() {
  const scheduler = globalThis.scheduler;
  if (scheduler && typeof scheduler.yield === 'function') {
    return scheduler.yield();
  }
  return new Promise((resolve) => {
    setTimeout(resolve, 0);
  });
}

Feel free to copy and paste this into your codebase. Or continue with using it as a module etc., as described below.

Installation

npm install yieldtomain

Usage

The library can be used in a bunch o' JavaScript environments:

ES Modules

import yieldToMain from 'yieldtomain';

async function processItems(items) {
  for (const item of items) {
    await yieldToMain();
    // Process item
  }
}

CommonJS

const yieldToMain = require('yieldtomain');

async function heavyTask() {
  for (let i = 0; i < 1000000; i++) {
    if (i % 1000 === 0) await yieldToMain();
    // Compute something
  }
}

Browser

<script src="dist/yieldToMain.browser.js"></script>
<script>
  async function nonBlockingTask() {
    for (let i = 0; i < 1000000; i++) {
      if (i % 1000 === 0) await yieldToMain();
      // Heavy computation
    }
  }
</script>

Demo

Check out the examples/heavy.html demo to see the difference between blocking and non-blocking computations. The demo includes:

  • A counter animation to visualize thread blocking
  • Pure computation examples (where "pure" means a JS loop without any DOM involvement)
  • DOM access in addition to computations
  • Multiple runs while reporting the median

A live version of the demo is right here.

Build Formats

The library is distributed in multiple formats:

  • dist/yieldToMain.esm.js - ECMAScript Module
  • dist/yieldToMain.cjs.js - CommonJS
  • dist/yieldToMain.browser.js - Minified browser build with an IIFE that provides a global yieldToMain() function

All builds include source maps and TypeScript type definitions.

API

function yieldToMain(): Promise<void>;

The function returns a Promise that resolves after yielding execution to the main thread. It uses:

  1. scheduler.yield() if available
  2. setTimeout(0) fallback

Development

# Install dependencies
npm install

# Build all formats and types
npm run build

# Run tests
npm test

# Run tests in watch mode
npm run test:watch

License

MIT

Further Reading

The yieldToMain() function is in no way my invention, I was just surprised it's not widely available in npm, so I decided to create a package for it. It's like a folk song that everyone knows, and yet it's origins are a little mirky. For more nitty-gritty details here's a selection of light (haha!) reading: