![Oracle Drags Its Feet in the JavaScript Trademark Dispute](https://cdn.sanity.io/images/cgdhsj6q/production/919c3b22c24f93884c548d60cbb338e819ff2435-1024x1024.webp?w=400&fit=max&auto=format)
Security News
Oracle Drags Its Feet in the JavaScript Trademark Dispute
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
main-thread-scheduling
Advanced tools
Fast and consistently responsive apps using a single function call
Fast and consistently responsive apps using a single function call
npm install main-thread-scheduling
The library lets you run computationally heavy tasks on the main thread while ensuring:
A real world showcase of searching in a folder with 10k notes, 200k+ lines of text, that take 50MB on disk and getting results instantly.
requestIdleCallback()
and requestAfterFrame()
for scheduling.navigator.scheduling.isInputPending()
API is available).idle
strategy are executed last so there
isn't some unexpected work that slows down the main thread after the background task is finished.yieldOrContinue(strategy)
function. The API has two more functions for more advanced cases.You can see the library in action in this CodeSandbox. Try removing the call to yieldToContinue()
and then type in the input to see the difference.
yieldOrContinue(strategy: 'interactive' | 'smooth' | 'idle', signal?: AbortSignal)
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('interactive')
for (const line of file.lines) {
fuzzySearchLine(line, query)
}
}
}
The library has two more functions available:
yieldControl(strategy: 'interactive' | 'smooth' | 'idle', signal?: AbortSignal)
isTimeToYield(strategy: 'interactive' | 'smooth' | 'idle', signal?: AbortSignal)
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('interactive')) {
render()
await yieldControl('interactive')
}
computeHeavyWorkOnValue(value)
}
}
There are three scheduling strategies available. You can think about them more easily by completing the sentence with one of the three words: "Scheduling the task keeps the page interactive
/smooth
/idle
."
interactive
– use this for things that need to display to the user as fast as possible. Every interactive
task is run for 83ms – this gives you a nice cycle of doing heavy work and letting the browser render pending changes.smooth
— use this for things you want to display to the user quickly but you still want for animations to run smoothly for example. smooth
runs for 13ms and then gives around 3ms to render the frame.idle
– use this for background tasks. Every idle task is run for 5ms.Web Workers are a great fit if you have: 1) heavy algorithm (e.g. image processing), 2) heavy process (runs for a long time, big part of the app lifecycle). However, in reality, it's rare to see people using them. That's because they require significant investment of time due to the complexity that can't be avoided when working with CPU threads regardless of the programming language. This library can be used as a gateway before transitioning to Web Workers. In most cases, you would discover the doing it on the main thread is good enough.
scheduler.postTask()
scheduler.postTask()
is available in some browsers today. postTask()
and main-thread-scheduling
do similar things. You can think of postTask()
as a lower level API — it might be the right choice in specific scenarios. Library owners might be interested in exploring the nuanced differences between the two. For most cases, main-thread-scheduling
provides a scheduleTask()
method that mimics that API of postTask()
while providing the extra benefits of the library.
Need help with performance or consulting on how to integrate main-thread-scheduling
in your project? Write to me at hello@astoilkov.com.
FAQs
Fast and consistently responsive apps using a single function call
The npm package main-thread-scheduling receives a total of 0 weekly downloads. As such, main-thread-scheduling popularity was classified as not popular.
We found that main-thread-scheduling demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
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.
Security News
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
Security News
The Linux Foundation is warning open source developers that compliance with global sanctions is mandatory, highlighting legal risks and restrictions on contributions.
Security News
Maven Central now validates Sigstore signatures, making it easier for developers to verify the provenance of Java packages.