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

perf: process: replace timeout with queueMicrotask #444

Merged
merged 1 commit into from
Nov 7, 2022

Conversation

ThaUnknown
Copy link
Contributor

@ThaUnknown ThaUnknown commented Nov 6, 2022

This old process polyfill uses a fix for setTimeouts which was originally made to work on some insanely niche smart TV's. We don't care about that as our minimal entry point is ESM anyways, so I replaced it with queueMicrotask which has very similar support ranges. This was first done by mathiasvr in process-fast so I changed the copyright to his, even tho it's just 1 line of code.

Why this matters?
I'll quote Endless on this matter:
The problem is with process browserified version of process.nextTick it uses a setTimeout(fn, 0) as a fallback. The setTimeout is delayed well enough in foreground to strangle the speed from 6-7000 kb/s down to just 400 kb/s when one tab is hidden "to save resources". if you instead use two windows the speed will be fast (normal) again.

setTimeout(fn, 0) can potentially be used, however as it is clamped to 4ms for timers nested more than 5 deep per the HTML spec, it does not make for a suitable polyfill for the natural immediacy of setImmediate. - MDN

- link

CPU usage aside, unlike setTimeout, queueMicrotask takes ~ microsecond to execute, so in those worst case scenarios it would be over x10000 faster than setTimeout.

Optionally if backwards compatibility is an issue, an absolutely tiny queueMicrotask polyfill could be used

Copy link
Member

@ije ije left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks!

@ije ije merged commit c03bf75 into esm-dev:main Nov 7, 2022
@ThaUnknown
Copy link
Contributor Author

I'm not certain why tests fail, demo has queueMicrotask so I don't see the issue

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

Successfully merging this pull request may close these issues.

None yet

2 participants