Study notes · 9% of the exam

Engine Performance and Memory

Know how V8 turns JavaScript into fast machine code, what makes it slow down again, and how to find and fix memory leaks with real measurements instead of folklore.

Key points

  1. 1

    V8 tiers code from the Ignition interpreter to the Sparkplug, Maglev and TurboFan compilers, using type feedback. Deoptimization falls back to the interpreter when assumptions break.

  2. 2

    Hidden classes (shapes) depend on property order. Create objects in one place with every field initialised, and avoid delete in hot paths so inline caches stay monomorphic.

  3. 3

    Arrays track elements kinds (SMI → DOUBLE → ELEMENTS, packed or holey), and transitions are one-way. Avoid holes and mixed types in hot arrays.

  4. 4

    GC is generational: a cheap scavenger handles the young generation, and concurrent mark-compact handles the old. Allocation rate drives GC frequency.

  5. 5

    Leaks are unintended references: uncleared timers and listeners, detached DOM nodes, unbounded caches, closures sharing a context. Use the three-snapshot technique and the retainers view.

  6. 6

    Watch for Big-O traps: includes inside loops (use a Set), shift queues, and spreading inside reduce. Move CPU-bound work to workers, and transfer buffers instead of copying them.

Common traps

  • A microbenchmark whose result is never used may be measuring nothing at all, because of dead-code elimination.

  • FinalizationRegistry callbacks may never run. Never rely on them to release resources.

  • heapUsed doesn't include Buffer contents, and --max-old-space-size doesn't limit off-heap memory.

Test yourself on Engine Performance and Memory

Ten questions, with the answer and explanation after each one.