Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Atomics.pause() will soon allow to micro-sleep in the main thread. #2499

Open
denis-migdal opened this issue Oct 18, 2024 · 0 comments
Open

Comments

@denis-migdal
Copy link
Contributor

denis-migdal commented Oct 18, 2024

Currently, there is no ways to synchronously wait or sleep in the main thread without an active wait, which is CPU intensive.
Well we can call a long synchronous function (with potentially a timeout, e.g. synchronous AJAX calls), but this is quite dirty.

Good news, Atomics.pause() will allow to sleep for a short amount of time (only for 10 to 100 nano-seconds, still >10,000x less CPU intensive). It won't free the CPU, but will avoid doing an intensive wait.

Atomics.pause() is now at the Stage 2.7 of TC39.
This means it might "soon" be included into browsers.

Though, it won't be possible to wait for a promise to finish as it will pause the main thread (so the micro-task queue won't run). Indeed, we can't get values from a promise without await or then.

One work-around could be to perform the asynchronous operation in a webworker, and to store a flag/result in a shared array ?
(There is also a shared struct that might comes to JS standard in order to better communicate with webworkers).

I'll try to keep you updated on this new JS feature.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant