open atlas
↑ Back to track
JavaScript Engine internals JSE · 00 · 01

What a JavaScript engine is

A JS engine turns source text into CPU work: parse, compile to bytecode, interpret, then JIT-optimise the hot parts — with a garbage collector reclaiming memory underneath. Engine vs runtime, and the map of this track.

JSE Foundations ◷ 10 min
Level
FoundationsJuniorMiddleSenior

You write arr.map(x => x * 2) and it runs. But nothing on your CPU understands map, arrow functions, or * on a JavaScript number. Something has to translate that text into the handful of integer and floating-point instructions the silicon actually executes — and decide, while your program runs, which parts are worth compiling to fast machine code. That something is the engine.

In the next ten minutes you will see exactly which stage is responsible for “slow JavaScript” — and why fixing it means working with the engine, not around it.

The engine is a program that runs your program

A JavaScript engine is a native program (V8 is roughly two million lines of C++) whose input is JavaScript source text and whose output is CPU work. It is not a black box: it is a pipeline of well-understood stages, and every stage has a performance personality you can observe and influence.

The naive mental model — “JavaScript is interpreted line by line” — has been wrong for over a decade. Modern engines are adaptive: they start cheap (interpret bytecode) and get expensive only where it pays off (compile to optimised machine code for code that runs many times). The whole track is about that adaptive machinery and how to stay on its fast paths.

The five stages

When you see a V8 flame graph or a Node.js profile that names these stages, you will know exactly where to look. Here is what each one does:

  1. Parser — reads the source text and builds an Abstract Syntax Tree (AST), the structured representation of your code. It also does a fast “pre-parse” pass to skip function bodies that have not been called yet (lazy parsing).
  2. Interpreter (Ignition in V8) — walks the AST once to emit compact bytecode, then executes that bytecode on a register-based virtual machine. This is where all code starts. It is fast to produce and memory-light.
  3. Optimising compilers (Sparkplug, Maglev, TurboFan) — for functions that run often (“hot”), the engine compiles bytecode to native machine code, using runtime type observations to specialise. Faster to run, slower and more memory-hungry to produce.
  4. Garbage collector (Orinoco in V8) — runs alongside everything, finding objects nothing references anymore and freeing their memory. You never call free(); the GC infers it.
  5. The heap and call stack — the memory where your objects (heap) and your function frames (stack) live. The shape and lifetime of what you put here drives the cost of every other stage.

The art of this track is reading those stages: why a function got compiled and then thrown away (deopt), why an object lookup is slow (shape divergence), why memory keeps climbing (a leak the GC cannot collect).

Engine vs runtime — a distinction that trips people up

The engine implements the ECMAScript language: numbers, objects, functions, prototypes, closures, Promise, garbage collection. That is all it implements. It has no idea what a setTimeout, a fetch, a DOM node, or a file handle is.

Those come from the runtime (the host): the browser, Node.js, Deno, Bun. The runtime embeds the engine and injects host objects and the event loop that schedules them. setTimeout is not JavaScript — it is a function the runtime hands to the engine.

Engine vs runtime: who provides what
Numbers, objects, closures, Promise
engine
Garbage collection, the heap
engine
setTimeout, queueMicrotask
runtime
fetch, DOM, fs, console
runtime
The event loop itself
runtime
V8 lines of C++ (approx.)
~2M

This is why the same engine (V8) powers Chrome, Node, Edge, and Deno with completely different capabilities: same language core, different host. We cover the event loop end-to-end in unit 07; for now, fix the boundary: the engine runs the language, the runtime supplies the world.

Quiz

Your Node script calls `setTimeout`. Which component actually implements `setTimeout`?

Quiz

Why does a modern engine interpret code first instead of compiling everything to machine code up front?

Order the steps

Order the stages a brand-new, never-run function passes through the first time it is called many times in a loop.

  1. 1 Parser builds the AST from source text
  2. 2 Ignition emits bytecode
  3. 3 The interpreter executes the bytecode (cold)
  4. 4 An optimising compiler produces specialised machine code (hot)
Why this works

Why call it an “engine” at all? The term is historical: early browsers had a component that “drove” the scripting, much as a game engine drives a game. The name stuck even though today’s engines are full optimising compilers with garbage collectors — closer to a small JVM than to a simple script driver.

Recall before you leave
  1. 01
    Name the five major subsystems of a JavaScript engine and what each does.
  2. 02
    What is the difference between the engine and the runtime? Give two examples of each.
  3. 03
    Why is 'JavaScript is interpreted and therefore slow' wrong?
Recap

A JavaScript engine is a native program that turns your source text into CPU work through five subsystems: a parser that builds an AST, an interpreter (Ignition) that emits and runs bytecode, optimising compilers (Sparkplug, Maglev, TurboFan) that compile hot code to machine code, a garbage collector (Orinoco) that reclaims memory, and the heap and stack where values live. The model is adaptive: start cheap, optimise only the hot paths. The engine implements only the ECMAScript language — the runtime (Chrome, Node, Deno, Bun) supplies timers, I/O, the DOM, and the event loop. Most “slow JavaScript” is the engine being knocked off a fast path, not interpretation overhead. Now when you see a deopt warning or a GC spike in a profile, you know which subsystem to blame and which unit of this track to reach for.

Practice

Start at the top. Tasks go easiest → hardest: recall a fact, apply it to a case, then a senior-level stretch. Open one, attempt it, then reveal.

recallapplystretch0 of 3 done
Connected lessons
appears again in184

Something unclear?

Ask a question about this lesson. Questions are anonymous and go straight to the author to make the lesson better.

shortcuts expand
search
K
prev piece
k
next piece
j
cycle tier
t
this menu
?
sources2
expand
  1. 01
  2. 02

Trademarks belong to their respective owners. Editorial reference only.