Skip to content

Stop an async WHILE loop waiting for a timer in each iteration to close #2979 - #2982

Merged
mathiasrw merged 1 commit into
AlaSQL:developfrom
20vision:fix/2979-while-yield
Nov 15, 2026
Merged

mathiasrw merged 1 commit into
AlaSQL:developfrom
20vision:fix/2979-while-yield

Conversation

@20vision

Copy link
Copy Markdown
Contributor

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(), then setImmediate, then a MessageChannel, with setTimeout(0) as the last choice. Results, BREAK and CONTINUE are unchanged.

test/test2979.js counts 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 checks BREAK and CONTINUE after 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-different is clean on both files. Not run: the browser tests.

Notes for review:

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.

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.
@mathiasrw mathiasrw added this to the v4.x Focus milestone Nov 15, 2026
@mathiasrw

Copy link
Copy Markdown
Member

Spot on. Thank you!

@mathiasrw
mathiasrw merged commit 7259a64 into AlaSQL:develop Nov 15, 2026
10 checks passed
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.

An async WHILE loop waits for a timer before each iteration

2 participants