perf: process: replace timeout with queueMicrotask #444
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 ofprocess.nextTick
it uses asetTimeout(fn, 0)
as a fallback. ThesetTimeout
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.- 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