frameworkEngineering & architecture

Browser Event Loop

The browser event loop coordinates queued tasks, microtasks, user input, scripts, and rendering while JavaScript jobs run to completion.

Download Markdown (.md)

01What it means

A browser event loop is the HTML processing model that schedules work for a window or worker agent. It selects tasks from task queues, performs microtask checkpoints, and gives window loops rendering opportunities. JavaScript within a job runs to completion, but event loops are not simply a single FIFO callback queue and do not necessarily map one-to-one to operating-system threads.[1][2]

Browser scheduling model A task runs, microtasks drain, then rendering may occur
  1. 01Select taskInput, timer, network, or other source
  2. 02Run taskJavaScript runs to completion
  3. 03Microtask checkpointPromise reactions and queued microtasks
  4. 04Rendering opportunityA window loop may update rendering

Select the next runnable task

JavaScript in a job runs to completion. The event loop is not simply one FIFO callback queue.

02Why it matters

Long tasks delay input and rendering even when network operations are asynchronous. Task and microtask ordering explains stale UI, timer behavior, promise callbacks, race-like bugs, and why resolved data may not appear immediately.

03When to use it

Use the model when debugging asynchronous order, responsiveness, timers, promises, event handlers, or rendering delays. Move CPU-heavy independent work to a worker and break cooperative main-thread work into bounded tasks where appropriate.

04How it works

  1. Platform activity queues tasks from sources such as input, timers, parsing, and networking.
  2. The event loop selects a runnable task and its JavaScript executes to completion.
  3. A microtask checkpoint drains queued promise reactions and other microtasks.
  4. A window loop may update rendering between tasks when a rendering opportunity occurs.
  5. Long work is measured and split, deferred, or moved off the main agent to protect responsiveness.

05A practical example

Example

A search interface parses a large response in one callback, freezing input despite using fetch. Profiling reveals a long main-thread task, so the team moves parsing to a worker and renders results in small batches.

06Where it breaks

Specifications define ordering constraints but browsers retain scheduling freedom. The model alone does not expose every engine thread, rendering stage, priority, or background throttling rule, and worker communication still has cost.

Common mistakes

  • Calling every queued callback a microtask.
  • Assuming a zero-delay timer runs immediately.
  • Creating an unbounded microtask chain that prevents rendering opportunities.
  • Believing asynchronous I/O makes its follow-up computation non-blocking.

07Sources

  1. HTML Standard: Event LoopsWHATWG · Living web standardHTML Standard: Event Loops
  2. JavaScript Execution ModelMDN Web Docs · Official technical documentationJavaScript Execution Model
View saved topics

Start typing to search titles, definitions, categories, and frameworks.