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

main-thread-scheduling

Package Overview
Dependencies
Maintainers
1
Versions
30
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

main-thread-scheduling

Consistently responsive apps while staying on the main thread

  • 2.0.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
4.4K
increased by21.81%
Maintainers
1
Weekly downloads
 
Created
Source


main-thread-scheduling

Consistently responsive apps while staying on the main thread

Build Status Test Coverage Test Coverage


Install

npm install main-thread-scheduling

Overview

The library ensures that:

  • the UI doesn't freeze
  • the user's computer fan doesn't spin
  • it can be easily integrated in an existing code base

This is accomplished through multiple strategies:

  • Stops task execution when user interacts with the UI. Uses navigator.scheduling.isInputPending() and fallbacks to IdleDeadline.
  • Global queue. Multiple tasks are executed one by one so increasing the number of tasks doesn't degrade performance linearly.
  • Sorts tasks by importance. Sorts by priority and gives priority to tasks requested later.
  • Urgent UI changes are given highest priority possible. Tasks with user-visible priority are optimized to deliver smooth UX.
  • Considerate about your existing code. Tasks with background priority are executed last so there isn't some unexpected work that slows down the main thread after the background task is finished.

Use Cases

  • You want to turn a synchronous function into a non-blocking asynchronous function. Avoids UI freezes.
  • You want to yield important results first and less urgent ones second. Improves perceived performance.
  • You want to run a background task that doesn't spin the fans. Avoids bad reputation.
  • You want to run multiple backgrounds tasks that don't pile up with time. Prevents death by a thousand cuts.

Why

Why rely on some open-source library to ensure a good performance for my app?

  • Not a weekend project. I have been working on this code for months. If you want to dive deeper, you can read the in-depth doc.
  • This is the future. Browsers are probably going to support scheduling tasks on the main thread in the future. Here is the spec.
  • Simple. 90% of the time you only need yieldOrContinue(priority) function. The API has two more functions for more advanced cases.
  • Aiming for high-quality with my open-source principles.

API

Note: If you want to understand how this library works under the hook and some of the details – read the in-depth doc.

yieldOrContinue(priority: 'background' | 'user-visible')

The complexity of the entire library is hidden behind this method. You can have great app performance by calling a single method.

async function findInFiles(query: string) {  
    for (const file of files) {
        await yieldOrContinue('user-visible')
        
        for (const line of file.lines) {
            fuzzySearchLine(line, query)
        }
    }
}

More complex scenarios

The library has two more functions available: yieldToMainThread(priority: 'background' | 'user-visible') and isTimeToYield(priority: 'background' | 'user-visible'). These two functions are used together to handle more advanced use cases.

A simple use case where you will need those two functions is when you want to render your view before yielding back control to the browser to continue its work:

async function doHeavyWork() {
    for (const value of values) {
        if (isTimeToYield('user-visible')) {
            render()
            await yieldToMainThread('user-visible')
        }
        
        computeHeavyWorkOnValue(value)
    }
}

Priorities

Currently there are only two priorities available: background and user-visible:

  • background – use this for background tasks. Every background task is run for 5ms.
  • user-visible – use this for things that need to display to the user as fast as possible. Every user-visible task is run for 50ms – this gives you a nice cycle of doing heavy work and letting the browser render pending changes.

If you have a use case for a third priority, you can write in this issue.

Alternatives

  • Web Workers

The problem this library solves isn't new. However, I haven't found a library that can solve this problem in a simple manner. Open an issue if there is such a library so I can add it here.

React has an implementation for scheduling tasks – react/scheduler. They plan to make it more generic but I don't know their timeline.

Keywords

FAQs

Package last updated on 31 Aug 2021

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