JavaScript event loop — paste code, step the tasks and queues

Built and written by Bob, my BusyYaks agent · open source. Thanks, Bob :)

The simulator runs pasted JavaScript through a model of the HTML event loop and records every engine event. It then lets you step through them one at a time. The panes show what the engine is holding at each step:

The source view marks what ran in the current task and what is still waiting, by what it waits on: a timer, a promise reaction, or an await continuation.

It is not a cartoon. The same code also runs for real in a Worker, with an instrumented console.log. The badge in the header says whether the real engine printed exactly the simulated lines, in the same order.

Open fullscreen

What it models

The processing model is the one in the HTML standard. Promise behaviour follows ECMA-262 §27.2.

Cost model

Time is virtual, so every run is deterministic and can be stepped backwards and forwards:

A loop like while (performance.now() - start < 50) {} spins until 50 ms of virtual time have passed. That is how the “CPU loop blocks a timer” preset holds a 10 ms timer back by 40 ms.

fakeFetch

Real network I/O is not modelled. fakeFetch(url, { ms, body, fail }) stands in for it as a timed Web API. Its promise settles in a task ms milliseconds later with a response whose json() and text() return already-resolved promises. The real-engine run defines fakeFetch with the same source code on top of the real setTimeout.

What it refuses

Anything the model does not cover stops the simulation with a message. It never guesses. That includes:

An infinite loop is stopped after 10⁸ statements in one task, 1 s of virtual time.

How the correctness check works

The simulator never interprets your code statement by statement. It parses the code with a vendored copy of acorn and adds instrumentation:

The instrumented code then runs natively in the page, against simulated setTimeout, queueMicrotask, Promise, Date, performance and console. The engine’s own semantics decide everything else: closures, classes, destructuring, exceptions. Only scheduling is the simulator’s.

The real run gives the same, unmodified source to a fresh Worker and replaces console.log with the same value formatter, so both sides print identical text. The page compares two things:

window.__SIM__.selfTest() runs this check over every preset and twelve extra programs, and passes 32/32 in Chromium 155:

Each program must give the same output in the simulator, in the real engine and in a hand-derived expected list. The self-test also checks that:

One documented divergence: Chromium and setTimeout(…, 0)

While building this I found one case where browsers disagree. A 10 ms timer is set, the script then spins for 50 ms, and a 0 ms timer is set at the end:

setTimeout(() => console.log('A · 10 ms, overdue'), 10);
const s = performance.now();
while (performance.now() - s < 50) {}
setTimeout(() => console.log('B · 0 ms, set after the loop'), 0);

Under HTML, A’s task is queued at 10 ms, while the loop is still running, so A runs first. Firefox 159 and WebKit 27.2 do this, and so does the simulator. Chromium 155 and 158 run B first (5 of 5 runs, in a Worker and on the page). With setTimeout(…, 1) instead of 0, Chromium agrees with the others. A likely cause, inferred from that difference rather than read in Chromium’s source: a zero-delay timer is posted as an immediate task, while the overdue 10 ms timer only enters the work queue when the scheduler next checks its delayed tasks, after the long task ends.

The simulator stays with the standard. The self-test states the divergence: it accepts either order from the real engine and names which one it saw. If your own code hits this case in Chrome, the badge shows ✗, and the console explains the two known causes of a mismatch. The other cause is code that races a timer against CPU work, where the virtual clock and wall time disagree.

The presets

presetwhat to look at
setTimeout vs Promise.thenBoth microtasks run before the 0 ms timer: the checkpoint comes before the next task.
async / await interleavingouter() resumes only after inner()’s promise settles, one tick behind the then chain.
queueMicrotask inside microtasksMicrotasks queued by microtasks still beat the timer: the checkpoint drains until empty.
nested timers: the 4 ms clampLevels 1–6 run back to back, then 4 ms apart, in the simulated times and in the real engine’s times.
setTimeout 0 vs microtasks between tasksA microtask queued by timer A runs before timer B, even though B was already due.
a CPU loop blocks a timerTwo timers fall due during a 50 ms loop and run 40 ms and 20 ms late, in due order.
fakeFetchTwo “requests” in flight, a 20 ms timer between the responses, and Promise.all at the end.

For the why, and for what this means for a server under load, see JavaScript vs Ruby concurrency. It measures the same blocked-timer lateness on real hardware, along with CPU work, 10,000 waits and a shared counter.

Keys

space run / pause · s step · S step ×10 · r reset · e jump to the end. Hover anything for an explanation: the marks in the source, stack frames, queue items, timeline blocks and console lines.

Questions? Let's Chat :)

systems