Skip to content

JavaScriptEventLoop: Add opt-in yieldInterval for host responsiveness - #26

Merged
krodak merged 1 commit into
mainfrom
kr/event-loop-yield-interval
Oct 16, 2026
Merged

krodak merged 1 commit into
mainfrom
kr/event-loop-yield-interval

Conversation

@krodak

@krodak krodak commented Oct 16, 2026 •

Copy link
Copy Markdown
Collaborator

Overview

JobQueue.runAllJobs() runs every queued job in a single microtask, including jobs enqueued along the way. Long-running async work blocks rendering, input, timers, and messages until it completes, and a Web Worker can't respond to messages in the meantime. Task.yield() doesn't help because the yielded task goes back into the same drain. Yielding from app code with Task.sleep only works if every ready task does it, and their timers then expire together.

This PR adds an opt-in yieldInterval. When set, a drain stops once the interval has passed and the remaining jobs continue in a new JavaScript task. The default (nil) keeps the current behavior, so latency-sensitive apps are unaffected.

JavaScriptEventLoop.shared.yieldInterval = .milliseconds(16)

1. Drain loop. After each job, runAllJobs() checks the elapsed time. Once the interval has passed and jobs remain, it schedules a continuation and returns with isSpinning still set, so new jobs wait for the continuation instead of starting another drain. Jobs are never interrupted.

2. Continuation. setImmediate (Node.js, Bun), else MessageChannel (browsers, Workers, Deno), else setTimeout(0). MessageChannel avoids the 4 ms clamp on nested timers; with setTimeout, the workload below was about 15% slower.

3. Clock. ContinuousClock, or performance.now() in Embedded Swift.

Measurements

A Swift/Wasm app that writes a 255k-node graph to SQLite, Chrome, medians:

No yield Task.sleep before each batch yieldInterval = 16 ms
Page: longest time with no serviced message 11.8 s 3.7 s 0.6 s
Dedicated Worker: p95 delay of a page→Worker message 11.9 s 3.1 s 0.1 s
Total write time 22.1 s 23.0 s 22.3 s

The remaining 0.6 s is a single synchronous job in the app, which no scheduler can split.

Testing

  • New JavaScriptEventLoopYieldIntervalTests (XCTest on Node.js):
    • yieldInterval is nil by default;
    • without an interval, a JS timer scheduled before 20 busy tasks runs only after all of them;
    • with 5 ms, it runs before the last one, both when the interval is set during the drain and before a new drain;
    • equal-priority tasks keep their order across yields.
  • make unittest
  • Embedded: JavaScriptEventLoop builds with the Embedded SDK, and a small Embedded client that sets the interval runs in Node.js and lets a JS timer run between jobs (without the interval, it does not).
  • ./Utilities/format.swift applied.

Docs: a doc comment on yieldInterval and one bullet in the JavaScript Interop Cheat Sheet.

@krodak
krodak force-pushed the kr/event-loop-yield-interval branch from 9f7a9ea to 0b3eb04 Compare October 16, 2026 07:14
@krodak
krodak marked this pull request as ready for review October 16, 2026 07:31
@krodak
krodak merged commit b8369d9 into main Oct 16, 2026
16 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.

1 participant