Repository navigation
Stop an async WHILE loop waiting for a timer in each iteration to close #2979 - #2982
Merged
Merged
Conversation
AlaSQL#2979 The async loop ran each iteration inside setTimeout(0), which browsers hold back for at least 4 ms once nested. The loop now runs the iterations that finish at once in a flat loop, and gives the event loop a turn only when 16 ms have gone by, using scheduler.yield(), setImmediate or MessageChannel, with setTimeout as the last choice.
Member
|
Spot on. Thank you! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Closes #2979.
The async WHILE loop ran each iteration inside
setTimeout(0). That made it slow in Node (388 ms against 10 ms for 300 iterations, as in the issue) and at least 4 ms per iteration in browsers.Iterations that finish at once now run in a flat loop, so the stack does not grow. The loop gives the event loop a turn only after 16 ms, using
scheduler.yield(), thensetImmediate, then aMessageChannel, withsetTimeout(0)as the last choice. Results,BREAKandCONTINUEare unchanged.test/test2979.jscounts timers (1000 iterations schedule fewer than 100), runs 100,000 iterations to check the stack, checks that the loop yields without help from its body, and checksBREAKandCONTINUEafter a yield. There are no wall-clock assertions.Run locally: the new tests fail before the change ("WHILE scheduled 1001 timers for 1000 iterations") and pass after it; the full suite gives 2560 pass, 213 skip, 0 fail;
prettier --list-differentis clean on both files. Not run: the browser tests.Notes for review:
yieldIntervalMsoption from Opt-in yield to the event loop between statements of an async batch #2980 is not part of this. The 16 ms is one constant (WHILE_YIELD_MS), so the option can replace it. If you plan one shared yield helper for Opt-in yield to the event loop between statements of an async batch #2980, I am glad to move this into it, or to close this PR in favour of yours.scheduler.yield(), an exception from the loop's condition or statement surfaces as an unhandled rejection, where a timer callback would throw uncaught.Disclosure: this change was written by an AI coding agent (Claude Code) that I run. It was tested as described above; no human has reviewed the code yet. If you would rather not get agent-written PRs, close it and that is fine.