Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

yieldy-for-loop

Package Overview
Dependencies
Maintainers
0
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

yieldy-for-loop

A small utility to process large loops without blocking the main thread

  • 1.0.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
0
Created
Source

Yieldy For Loop

A tiny utility to process large loops in a non-blocking way by yielding periodically, ensuring your UI stays responsive even under heavy iteration.

Inspired by “Breaking up with long tasks, or how I learned to group loops and wield the yield” (PerfPlanet 2024).

Features

  • Periodically yields to the browser to keep your app’s UI smooth.
  • Automatically adjusts yield threshold if the document is hidden.
  • Leverages requestAnimationFrame and a small timeout race.
  • Optionally calls scheduler.yield() when available.
  • Works with any iterable (not just arrays).

Installation

npm install yieldy-for-loop

Usage

import yieldyForLoop from "yieldy-for-loop";

const items = Array.from({ length: 100000 }, (_, i) => i);

function processItem(item) {
  // Your heavy computation
  console.log("Processing item:", item);
}

(async function main() {
  console.log("Starting yieldy loop...");

  await yieldyForLoop(items, processItem, {
    fps: 30,            // optional, default is 30
    hiddenThreshold: 500 // optional, default is 500
  });

  console.log("Done processing!");
})();

API

yieldyForLoop<T>(
  items: Iterable<T>, 
  processFn: (item: T) => void, 
  options?: {
    fps?: number;              // default 30
    hiddenThreshold?: number;  // default 500 ms
  }
): Promise<void>;

Parameters:

  • items: An iterable collection of items you want to process.
  • processFn: A function that processes each item in the collection.
  • options:
    • fps (number): Target frames per second; used to calculate how long each batch can run before yielding.
    • hiddenThreshold (number): Yield threshold (in ms) if the document is hidden.

How It Works

  1. Time Slice: It calculates a BATCH_DURATION from the desired FPS (e.g., 30 FPS = 33 ms).
  2. Yield Check: Before each item, it checks if the time since the last yield exceeds the threshold (based on whether the document is visible or hidden).
  3. Yielding:
    • If the document is hidden, it does a quick setTimeout to yield.
    • If the document is visible, it waits for either a 100 ms timeout or the next animation frame, then optionally uses the Scheduler API (scheduler.yield()) if available.
  4. Process the Item: Only after yielding (if needed), it calls your processFn.

Why Use It?

When you have long-running loops—like processing large arrays or performing expensive computations on each iteration—the main thread can get blocked, causing the UI to freeze. By periodically yielding control to the browser, you let the UI stay responsive, handle user interactions, and remain smooth.

Inspiration

Contributing

Contributions, issues, and feature requests are welcome! Feel free to open an issue or pull request on GitHub.

License

MIT

Keywords

FAQs

Package last updated on 14 Jan 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc