The V8 Engine & Event Loop

Node combines the V8 engine, libuv, and built-in APIs into one runtime.

Node.js is more than just JavaScript. Under the hood it glues together several parts:

  • V8 — Google's engine that compiles and runs your JavaScript.
  • libuv — a C library that provides the event loop and a thread pool for non-blocking I/O.
  • Node APIs — built-in modules like fs, http, and path.

Non-blocking by design

Node runs your code on a single thread, but slow work (reading files, network requests) is handed off so the program never freezes while waiting. This is what makes Node fast for I/O-heavy apps.

Node.js runtime is V8 plus libuv plus built-in APIs on top of the operating systemYour JavaScript codeNode.js RuntimeV8 Engineruns your JSlibuvevent loop+ thread poolNode APIsfs, http, path...Operating System
Node = V8 (runs JS) + libuv (event loop) + built-in APIs.

Example

Example · javascript
// Node keeps running while slow work happens in the background
console.log('Start');
setTimeout(() => console.log('Runs later, after 0ms delay'), 0);
console.log('End');
// Output order: Start, End, Runs later...

When to use it

  • An API server handles 10 000 concurrent HTTP requests without spawning extra threads because the event loop offloads I/O to libuv.
  • A monitoring dashboard streams live metrics to many WebSocket clients simultaneously on a single Node process thanks to non-blocking I/O.
  • A file-processing service reads thousands of log files in parallel, relying on libuv's thread pool so the event loop is never blocked.

More examples

Non-blocking setTimeout demo

Shows that even a 0 ms timeout is deferred until the current call stack is empty.

Example · js
console.log('Start');
setTimeout(() => console.log('Timer fired'), 0);
console.log('End');
// Output: Start -> End -> Timer fired

Async file read won't block

Demonstrates that `fs.readFile` hands off to libuv and the event loop continues executing synchronous code.

Example · js
const fs = require('fs');
console.log('Before read');
fs.readFile('data.txt', 'utf8', (err, txt) => {
  console.log('File contents received');
});
console.log('After read call -- event loop free');

Blocking vs non-blocking comparison

Contrasts the synchronous (blocks the loop) and asynchronous (preferred) file-reading APIs.

Example · js
const fs = require('fs');
// Blocking -- freezes the event loop
const data = fs.readFileSync('data.txt', 'utf8');
console.log('Sync done');

// Non-blocking -- preferred for servers
fs.readFile('data.txt', 'utf8', (err, d) => console.log('Async done'));

Discussion

  • Be the first to comment on this lesson.