Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Explaining What V8 Learns

Explaining What V8 Learns

In this lesson, you see what V8, the JavaScript engine inside Node, learns about your code while it runs. Your profile pointed at the scoring work, and your Python teammate has a theory: JavaScript is interpreted, so every call costs the same.

That picture comes from the classic view of CPython. score.py replays the evening’s three rows a hundred thousand times, a toy stand-in for a day of traffic:

Language: Python
rows = [
    {"team": "Falcons", "points": 3},
    {"team": "Owls", "points": 2},
    {"team": "Falcons", "points": 4},
]

def add_points(total, points):
    return total + points

total = 0
for _ in range(100_000):
    for row in rows:
        total = add_points(total, row["points"])
print(total)

Each row’s points go through add_points(), and dis shows its addition as one BINARY_OP instruction. The script prints the total:

Language: Program Output
900000

In JavaScript, the rows are object literals in an array, and addPoints() is a function declaration. JavaScript has no range(); here a while loop counts n up to 100000, around a for...of loop over the rows:

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

00:00 In this lesson, you’ll see what V8, the JavaScript engine inside Node, learns about your code while it runs. Your profile pointed at the scoring work, and your Python teammate has a theory: JavaScript is interpreted, so every call costs the same.

00:17 That picture comes from the classic view of CPython. score.py replays the evening’s three rows a hundred thousand times. Each row’s points go through add_points(), and dis shows its addition as one BINARY_OP instruction.

00:32 The script prints 900000.

00:36 In JavaScript, the rows are object literals in an array, and addPoints() is a function declaration. JavaScript has no range(); here a while loop counts n up to 100000, around a for...of loop over the rows.

00:52 The port prints the same total, and nothing in it says how each call ran.

00:59 When does addPoints() start running as machine code: from the first call, never, or partway through? Your turn: pause the video and predict.

01:14 V8’s --trace-opt flag logs each time V8 optimizes a function. Partway through: V8 marked addPoints() for optimization, hot and stable, then Maglev, a V8 compiler, compiled it.

01:30 Hot means called often; stable means what it sees has stopped changing. So later calls ran different instructions from the first, with the same total. These lines are one run on one machine; another run or Node version may print others.

01:49 V8 compiled the code to bytecode, as CPython does, and its interpreter, Ignition, ran the first calls. Meanwhile, it recorded feedback: what each operation saw, such as small integers at total + points.

02:04 Once addPoints() was hot, Maglev, an optimizing compiler, used that feedback to build machine code.

02:12 Today’s V8 has four tiers: Ignition; Sparkplug, a compiler that doesn’t optimize; then Maglev and TurboFan, which do. Each later tier compiles more slowly and builds faster code, so only code that stays hot reaches it.

02:29 Those names are this V8’s, not JavaScript’s. So JavaScript isn’t only interpreted, nor compiled ahead of time like C.

02:39 That machine code is a bet on the feedback. A late row scores a draw: 0.5 points to the Owls. late.mjs adds it with the same addPoints(); both totals are right.

02:54 --trace-deopt, which logs when V8 abandons optimized code, shows a bailout in addPoints(). The reason is “not a Smi”: V8’s compact form for whole numbers in a fixed range, such as 900000, not Python’s small-int cache.

03:11 Maglev’s code had checked for Smis, and 0.5 failed. So V8 dropped back to its bytecode, which handles any value. Specializing behind checks like that is guarded specialization; abandoning it is deoptimization.

03:28 With 0.5 in its feedback, V8 can optimize it again later.

03:33 Back in Python, CPython 3.11 and later do a smaller version of this. Importing score runs the replay; then dis with adaptive=True shows BINARY_OP_ADD_INT, an int addition with its own check.

03:49 So even in Python, later calls run a different instruction from the first. Unlike V8, CPython keeps the instruction when one 0.5 fails that check, and does one general addition.

04:02 Python 3.15 can include an experimental just-in-time compiler, off by default, that compiles hot sequences of those instructions. So it isn’t compiled versus interpreted: default CPython specializes single instructions in its interpreter; V8 compiles whole hot functions to machine code.

04:24 So here’s what to tell your teammate: V8 runs bytecode first, compiles hot functions from what they’ve seen, and returns to bytecode when a check fails.

04:34 Tiers change timing, never results. Early calls run before V8 has learned anything, so a JavaScript developer measures warmed-up medians, as in your fair comparison.

04:47 They record process.versions, since the tiers are V8’s, and read a trace line as one run’s observation.

04:55 Next up is the text lesson Keeping Record Construction Consistent. There, you’ll see why the same keys alone don’t prove V8 lays records out alike.

Become a Member to join the conversation.