Stop 02 · Node.js FundamentalsTopics · 2/18

V8 Engine

Part of the Backend Developer Roadmap

In short

V8 turns your JavaScript into machine code, and it's clever about it. A little knowledge of how it optimises explains why some Node code flies and some mysteriously crawls.

Google's open-source JavaScript engine (also used in Chrome) that compiles JS to machine code — the actual execution engine underneath every Node.js program.

The mascot tuition teacher explains at a blackboard while holding up a prepared answer sheet.

Think of it like this

The tuition teacher's ready answers

A new question gets a slow, line-by-line explanation. If the same question keeps coming, the teacher writes a ready answer sheet and just hands it over. V8 does this too: it interprets code first, then compiles the busy parts into fast machine code.

Watch it happen

How V8 runs a function

Source
Parse
Bytecode
Hot code
Optimised
Deopt

1/6V8 receives your JavaScript from Node as plain text.

The big ideas

What you need to know

Just-in-time compilation

V8 compiles while your program runs, spending effort only on code that runs often.

Objects have shapes

Objects created with the same keys in the same order share a hidden 'shape', which lets V8 read properties fast.

Garbage collection

V8 frees memory you no longer use. Holding references forever, like an ever-growing cache, causes memory leaks.

Memory has a limit

The heap has a size limit. Raise it with --max-old-space-size, but first check you're not leaking.

See it in code

Walk through the code

js
1// shapes.js: keep objects the same shape so V8 can optimise2function makeUser(name, age) {3  return { name, age }; // same keys, same order, every time4}5 6function totalAge(users) {7  let sum = 0;8  for (const u of users) sum += u.age;9  return sum;10}11 12const users = Array.from({ length: 1e6 }, (_, i) => makeUser("u" + i, i % 60));13console.time("sum");14console.log(totalAge(users));15console.timeEnd("sum");16// Try: node --trace-opt shapes.js

1/4Every user object gets the same keys in the same order, so they all share one shape.

The --trace-opt flag prints a line whenever V8 decides to optimise a function. The exact output changes between versions, so just look for your function names.

Save yourself the pain

Common mistakes

Oops

Adding keys in random orders or deleting keys on objects in hot code.

Do this instead

Create objects with all fields up front, using null for 'not yet'. One shape keeps V8 on its fast path.

Oops

Rewriting every line based on V8 tricks you read about.

Do this instead

Write clear code first. Profile with node --cpu-prof, then optimise only the functions that really are slow.

Your mission

Watch V8 at work

~20 min

Quick check

V8 optimised a function for numbers, and then you call it with a string. What happens?

Go deeper

  1. 1Read a short explainer on how V8 compiles and optimizes JavaScript.
  2. 2Learn what JIT (Just-In-Time) compilation means at a high level.
  3. 3Understand that Node.js itself is just V8 plus a set of additional APIs (libuv, Node APIs).

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge