Skip to content

Commit 1493bac

Browse files
committed
fix: correct event loop algorithm order and improve technical accuracy
- Fix pseudocode to match HTML spec: task -> microtasks -> render (was render before task) - Add clarification that 4ms rule output varies by browser/system - Replace misleading 'separate threads' claims with accurate description - Fix 'continuously monitors' to 'coordinates execution when stack empty' - Add setTimeout third parameter as alternative closure fix - Clarify requestIdleCallback timing (runs if idle time remains) - Update Loupe tool links from HTTP to HTTPS
1 parent 383e0a1 commit 1493bac

1 file changed

Lines changed: 30 additions & 21 deletions

File tree

docs/concepts/event-loop.mdx

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ Even with a 0ms delay, `Timeout` prints last. The answer lies in the **[event lo
3939

4040
## What is the Event Loop?
4141

42-
The **event loop** is JavaScript's mechanism for executing code, handling events, and managing asynchronous operations. It continuously monitors the call stack and callback queues, pushing queued tasks to the stack when it's empty. This enables non-blocking behavior despite JavaScript being single-threaded.
42+
The **event loop** is JavaScript's mechanism for executing code, handling events, and managing asynchronous operations. It coordinates execution by checking callback queues when the call stack is empty, then pushing queued tasks to the stack for execution. This enables non-blocking behavior despite JavaScript being single-threaded.
4343

4444
### The Restaurant Analogy
4545

@@ -177,7 +177,7 @@ To understand the Event Loop, you need to see the full picture:
177177
│ │ setTimeout() setInterval() fetch() DOM events │ │
178178
│ │ requestAnimationFrame() IndexedDB WebSockets │ │
179179
│ │ │ │
180-
│ │ (These run in SEPARATE THREADS managed by the browser!) │ │
180+
│ │ (These are handled outside of JavaScript execution!) │ │
181181
│ └─────────────────────────────────────────────────────────────────┘ │
182182
│ │ │
183183
│ │ callbacks │
@@ -267,7 +267,7 @@ To understand the Event Loop, you need to see the full picture:
267267
- Timers
268268
- Child processes
269269

270-
These run in **separate threads** managed by the browser/Node.js, allowing JavaScript to remain non-blocking.
270+
These are handled by the browser/Node.js runtime outside of JavaScript execution, allowing JavaScript to remain non-blocking.
271271
</Accordion>
272272

273273
<Accordion title="Task Queue (Macrotask Queue)">
@@ -502,29 +502,30 @@ Even though the second promise is created AFTER setTimeout was registered, it st
502502
### The Event Loop Algorithm (Simplified)
503503

504504
```javascript
505-
// Pseudocode for the Event Loop
505+
// Pseudocode for the Event Loop (per HTML specification)
506506
while (true) {
507-
// 1. Run all synchronous code until call stack is empty
507+
// 1. Process ONE task from the task queue (if available)
508+
if (taskQueue.hasItems()) {
509+
const task = taskQueue.dequeue();
510+
execute(task);
511+
}
508512

509-
// 2. Process ALL microtasks
513+
// 2. Process ALL microtasks (until queue is empty)
510514
while (microtaskQueue.hasItems()) {
511515
const microtask = microtaskQueue.dequeue();
512516
execute(microtask);
513517
// New microtasks added during execution are also processed!
514518
}
515519

516-
// 3. Render if needed (browser decides)
520+
// 3. Render if needed (browser decides, typically ~60fps)
517521
if (shouldRender()) {
522+
// 3a. Run requestAnimationFrame callbacks
523+
runAnimationFrameCallbacks();
524+
// 3b. Perform style calculation, layout, and paint
518525
render();
519526
}
520527

521-
// 4. Process ONE task
522-
if (taskQueue.hasItems()) {
523-
const task = taskQueue.dequeue();
524-
execute(task);
525-
}
526-
527-
// 5. Repeat
528+
// 4. Repeat (go back to step 1)
528529
}
529530
```
530531

@@ -616,8 +617,8 @@ setTimeout(function run() {
616617
}
617618
}, 0);
618619

619-
// Typical output: [1, 1, 1, 1, 4, 9, 14, 19, 24, 29]
620-
// First 4 are fast, then 4ms minimum kicks in
620+
// Typical output (varies by browser/system): [1, 1, 1, 1, 4, 9, 14, 19, 24, 29]
621+
// First 4-5 are fast, then 4ms minimum kicks in
621622
```
622623

623624
<Warning>
@@ -813,8 +814,8 @@ One Event Loop Iteration:
813814
│ a. Run requestAnimationFrame callbacks ← HERE! │
814815
│ b. Render/paint the screen │
815816
├─────────────────────────────────────────────────────────────────┤
816-
│ 4. If idle time available:
817-
│ Run requestIdleCallback callbacks
817+
│ 4. If idle time remains before next frame:
818+
│ Run requestIdleCallback callbacks (non-essential work)
818819
└─────────────────────────────────────────────────────────────────┘
819820
```
820821

@@ -1009,7 +1010,7 @@ for (let i = 0; i < 3; i++) {
10091010
// Output: 0, 1, 2
10101011
```
10111012

1012-
**Fix with closure:**
1013+
**Fix with closure (IIFE):**
10131014
```javascript
10141015
for (var i = 0; i < 3; i++) {
10151016
((j) => {
@@ -1018,6 +1019,14 @@ for (var i = 0; i < 3; i++) {
10181019
}
10191020
// Output: 0, 1, 2
10201021
```
1022+
1023+
**Fix with setTimeout's third parameter:**
1024+
```javascript
1025+
for (var i = 0; i < 3; i++) {
1026+
setTimeout((j) => console.log(j), 0, i);
1027+
}
1028+
// Output: 0, 1, 2
1029+
```
10211030
</Accordion>
10221031

10231032
### Question 5: What's Wrong Here?
@@ -1446,7 +1455,7 @@ requestAnimationFrame(() => {
14461455

14471456
The best way to truly understand the Event Loop is to **see it in action**.
14481457

1449-
<Card title="Loupe - Event Loop Visualizer" icon="play" href="http://latentflip.com/loupe/">
1458+
<Card title="Loupe - Event Loop Visualizer" icon="play" href="https://latentflip.com/loupe/">
14501459
Created by Philip Roberts (author of the famous "What the heck is the event loop anyway?" talk). This tool lets you write JavaScript code and watch how it moves through the call stack, Web APIs, and callback queue in real-time.
14511460
</Card>
14521461

@@ -1657,7 +1666,7 @@ Watch how:
16571666

16581667
## Tools
16591668

1660-
<Card title="Loupe - Event Loop Visualizer" icon="play" href="http://latentflip.com/loupe/">
1669+
<Card title="Loupe - Event Loop Visualizer" icon="play" href="https://latentflip.com/loupe/">
16611670
Interactive tool by Philip Roberts to visualize how the call stack, Web APIs, and callback queue work together. Write code and watch it execute step by step.
16621671
</Card>
16631672

0 commit comments

Comments
 (0)