█
LastWrite
  • > Curriculum
  • > Pricing
  • > For Educators
  • > About
  • > Contact
Log InGet Started

Questions, concerns, bug reports, or suggestions? We read every message, write to us at [email protected].

More ways to reach us →
LastWrite

Structured computer science lessons for aspiring developers and security professionals.

[email protected]

(201) 785-7951

Mon–Fri, 9 AM–5 PM EST

Learn

  • Curriculum
  • Pricing

Company

  • About
  • For Educators & Schools
  • Contact Us

Legal

  • Terms of Service
  • Privacy Policy
© 2026 LastWrite. All rights reserved.
Curriculum/Programming Languages/JavaScript (Standalone Deep Dive)/The JavaScript Engine and Event Loop
50 minAdvanced

The JavaScript Engine and Event Loop

After this lesson, you will be able to: Understand the JS engine: how V8 works, the call stack, the event loop, task queue vs microtask queue.

Most JS bugs in async code come from misunderstanding the event loop. This lesson is the mental model that ends the guessing.

This is a free introductory lesson. No purchase required.

V8 in one paragraph

V8 (Node, Chrome) parses JS, generates bytecode (Ignition interpreter), then JIT-compiles hot code (TurboFan / Sparkplug). Hidden classes + inline caches make property access fast, when the engine can predict your object's shape. Deoptimization happens when shapes change unpredictably. That's why monomorphic code (objects with consistent shapes) outperforms polymorphic.

The event loop in action

Predict the output BEFORE reading the explanation.

tsx
console.log("1");
setTimeout(() => console.log("2"), 0);
Promise.resolve().then(() => console.log("3"));
console.log("4");
// Output: 1 4 3 2
//
// 1, synchronous, runs immediately
// 4, synchronous, runs immediately
// 3, Promise.then is a MICROTASK; runs before the next macrotask
// 2, setTimeout callback is a MACROTASK; runs in the next loop tick

Microtasks vs macrotasks

Macrotasks: setTimeout, setInterval, I/O callbacks, MessageChannel. One per loop tick. Microtasks: Promise.then, queueMicrotask, MutationObserver. ALL microtasks drain before the next macrotask. Implication: if you `await` in a tight loop, you can starve macrotasks (UI freezes in the browser; setTimeout never fires).

💡 Why this matters in interviews

Senior JS interviews routinely ask 'what's the output of this code?' for snippets that mix sync + Promise + setTimeout. Knowing the microtask vs macrotask distinction is the difference between guessing and answering with confidence.

Common mistakes only experienced JS devs avoid

Treating Promise.then as synchronous. Assuming setTimeout(0) is 'immediate'. It's after current sync code + all microtasks. Mixing async/await with .then() callbacks unnecessarily. Pick one style. Calling expensive sync code from within Promise.then, blocks every other microtask until done.

Quick Check

What's the output? `setTimeout(() => console.log(1), 0); Promise.resolve().then(() => console.log(2)); console.log(3);`

Pick the order.

Back to JavaScript (Standalone Deep Dive)
Closures and Scope→