liveBook · Manning

Single-threaded execution model

As explained in Secrets of the JavaScript Ninja, Third Edition

Single-threaded execution means JavaScript code executes in one thread of control at a time.

The single-threaded execution model means JavaScript runs one piece of code at a time. In a browser, event handlers share that one-at-a-time execution path. section 1.1 section 4.3.1

For browser events:

  • The browser places detected events in a queue in detection order, then processes the event at the head. section 4.3.1
  • While one handler runs, other events wait. A long-running handler can make a web application unresponsive. section 4.3.1

One teller window

Imagine a bank line with only one teller window: customers (events) wait their turn while one is being served. Likewise, a slow handler holds up events behind it. section 4.3.1

Figure 4.6 In the event-handling phase, all events (whether coming from the user, such as mouse clicks and key presses, or coming from the server, such as fetch events) are queued up as they occur and are processed as the single thread of execution allows.

The figure shows user events and server events entering a queue and being processed by the single thread. section 4.3.1

How Secrets of the JavaScript Ninja, Third Edition covers it

How deep it goes
Defines single-threaded execution and outlines event-queue processing; defers detailed event-loop behavior.
Examples
Uses a one-teller bank line analogy and cites clicks, key presses, and fetch events.
What you can do after
Diagnose why lengthy event handlers leave web applications unresponsive by tracing queued events waiting their turn.
How it connects
Connects event handling and event queues to callbacks, promises, and workers.

Also explained in

Compare how 3 books explain single-threaded execution model →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Secrets of the JavaScript Ninja, Third Edition. The section links point to the parts of the book it draws on.