Skip to content

An async WHILE loop waits for a timer before each iteration #2979

Closed

Description

@mathiasrw

An async WHILE loop waits for a setTimeout(..., 0) before each iteration. This makes the loop much slower than the sync loop.

const sql = 'DECLARE @i INT; SET @i = 0; WHILE @i < 300 SET @i = @i + 1';
alasql(sql); // 10 ms
await alasql.promise(sql); // 388 ms in Node 24

Browsers make nested timers wait at least 4 ms. In a browser, 300 iterations thus take at least 1.2 s, and 10 000 iterations take at least 40 s.

The timer is in yy.While.prototype.execute in src/statements/while.js. It gives the event loop a turn after each iteration. That is useful, but a timer is the slowest way to do it.

Proposal

  • Use a faster yield. Use scheduler.yield() if the browser has it. If not, use a MessageChannel in the browser and setImmediate in Node. Use setTimeout(0) only as the last choice.
  • Yield only when 16 ms have gone by since the last yield, and not after each iteration.
  • The async loop must continue to give the event loop a turn by itself. Users must not have to add a yield to the loop body.
  • If the option alasql.options.yieldIntervalMs comes (Opt-in yield to the event loop between statements of an async batch #2980), the loop uses its value in place of 16 ms. With that option off, the loop continues to use 16 ms.

The results do not change. Only the time changes.

Test

An async WHILE with 1000 iterations is complete in less than 10 times the time of the sync loop.

Activity

  1. added this to the v4.x Focus milestone on Nov 11, 2026
  2. added a commit that references this issue on Nov 16, 2026
    7259a64
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions