JavaScript runtime model (browser & V8)
Core details
Single main thread per realm (typical tab or Node worker): call stack runs synchronous code; async work completes later on queues.
| Piece | Role |
|---|---|
| Call stack | currently executing functions |
| Heap | objects (GC-managed) |
| Web APIs / libuv | timers, network, I/O callbacks scheduled |
| Task queues | macrotasks (e.g. timer, I/O completion) |
| Microtask queue | Promise.then, queueMicrotask—runs after stack clears, before next macrotask |
Event loop (browser sketch): process microtasks until empty → take one macrotask → repeat → render when needed.
Node: similar loop with phases (timers, poll, check for setImmediate); process.nextTick runs before other microtasks—easy to starve I/O if abused.
Understanding
Closure = function + captured env; hoisting = var/function decl creation before execution per scope rules (let/const TDZ different).
Interview line: “await yields the function; microtasks chain before the next timer callback.”
Senior understanding
| Trap | Narrative |
|---|---|
| CPU-bound on main thread | chunk work or Worker—async doesn’t parallelize CPU |
Mass nextTick | can block I/O phase |
Diagram
Loading diagram…
See also
Last updated on
Spotted something unclear or wrong on this page?