Skip to content

Opt-in yield to the event loop between statements of an async batch #2980

Open

Description

@mathiasrw

An async batch of statements blocks the browser page until the batch is complete. This issue proposes an option that gives the event loop a turn between two statements.

Problem

alasql.promise runs the statements of a batch as a chain of promises. All statements run as microtasks. Input, rendering and timers cannot run until the last statement is complete.

setTimeout(() => console.log('timer'), 0);
await alasql.promise(['CREATE TABLE t (a INT)', 'INSERT INTO t VALUES (1)', 'SELECT * FROM t']);
await alasql.promise('CREATE TABLE u (a INT); INSERT INTO u VALUES (1); SELECT * FROM u');
// AlaSQL writes 'timer' here, after the two batches

Proposal

Add two options. The default is off.

alasql.options.yieldIntervalMs = 16; // default false
alasql.options.yieldFn = null; // default null

yieldIntervalMs

  • A number turns the yield on.
  • After each statement except the last, AlaSQL yields if this number of milliseconds has gone by since the start of the run or the last yield.
  • 0 yields after each statement except the last.
  • A batch of many small statements yields only some times. This keeps the cost of the yield low.
  • AlaSQL uses scheduler.yield() if the browser has it. If not, AlaSQL uses a MessageChannel in the browser, setImmediate in Node, and setTimeout(0) as the last choice. Browsers make nested timers wait 4 ms, so setTimeout is last.
  • Each run keeps its start time and the time of its last yield. AlaSQL keeps no global yield state.

yieldFn

Example 1. Wait for the next frame, so that the browser shows a progress bar between two statements.

alasql.options.yieldIntervalMs = 16;
alasql.options.yieldFn = () => new Promise(resolve => requestAnimationFrame(() => resolve()));

Example 2. Count the yields in a test.

let yields = 0;
alasql.options.yieldIntervalMs = 0;
alasql.options.yieldFn = async () => {
	yields++;
};
await alasql.promise(['SELECT 1', 'SELECT 2', 'SELECT 3']);
// yields is 2

Scope

  • The options apply to each async run of more than one statement. This includes alasql.promise([...]), alasql.promise('A; B; C') and alasql(sql, params, cb).
  • A sync call does not yield.
  • With the options off, the behaviour does not change. This is not a breaking change.

Limits

  • The yield does not split one statement. A SELECT that runs for 2 s blocks for 2 s.
  • A yield inside a scan needs an async copy of the compiled query code and the join and group loops. This makes the hot loops slower and gives two code paths to keep. A sort cannot stop in the middle. Thus this issue does not include a yield inside a statement.
  • For a long single query, use the worker build (dist/alasql-worker.js), or report the query as a performance issue. Comma join is slow when a table is filtered by IN or OR #2916 went from more than 60 s to 14 ms.

Risks

Where

  • src/engine/promise.js, for the array form.
  • adrun in src/engine/core.js, for more than one statement in one string and for the callback form.

Tests

  • Options off. A timer set before a batch runs after the batch.
  • yieldIntervalMs = 0. A timer set before a batch runs between its statements.
  • yieldIntervalMs = 0 with yieldFn. AlaSQL calls yieldFn one time between each two statements. AlaSQL does not call it for one statement.
  • A USE from other code during a yield does not change the database of the remaining statements.
  • The results and errors are the same with the options on and off.

Documentation

  • Add a wiki page for the two options, with the two examples, the limits above and a link to the worker build.
  • Add a short mention to the README, near alasql.promise.

Possible next step, a YIELD statement

A YIELD statement marks a point where AlaSQL can yield. Do this in a different issue after the options.

BEGIN
  INSERT INTO t SELECT * FROM big1;
  YIELD;
  INSERT INTO t SELECT * FROM big2;
END

Grammar

  • Add YIELD to the non-reserved words, as CONTINUE is. A column with the name yield then continues to work. continue works as a column name today (checked).
  • Thus the statement is not a breaking change.

Behaviour

  • YIELD follows the options. With yieldIntervalMs off, YIELD does nothing.
  • With yieldIntervalMs set, YIELD yields if that number of milliseconds has gone by since the start of the run or the last yield. It uses the same run times as the yield between statements.
  • In a sync call, YIELD does nothing.
  • YIELD gives 1 in the results array, as DECLARE and SET do.

Where it is useful

  • At the top level of a batch, the options already yield between statements. There, YIELD adds nothing.
  • Inside BEGIN END and IF, there is no yield between statements. There, YIELD adds a yield point.
  • Inside WHILE, the loop yields by itself after An async WHILE loop waits for a timer before each iteration #2979. YIELD is not necessary there.

Code

  • The async code for the procedural statements already exists. BeginEnd runs its statements one by one with a callback, If sends the callback on, and While calls the callback after each iteration.
  • Thus yy.Yield.prototype.execute(databaseid, params, cb) can do the yield and then call cb. The procedural statements need no change.
  • execute must get the run times. Today it gets only databaseid, params and cb, so the run context must go through BeginEnd, If and While.

Activity

  1. added this to the v5.x Focus milestone on Nov 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions