Join the super secret V8 club and become a JavaScriptbender

The contents here is created from the official CascadiaJS Page

Overview

Vladimir de Turckheim image

Abstract

Multiple features in V8 and Node.js are not fully documented or less known by most users. These features can be used to optimize the execution of code or debug it with superpowers.

Notes

@polesdesfetes (Private)

Staff engineer at Data Dog.

V8 Engine

  • Takes JS as input and executes it
  • Createed by Google, open source, C++
  • good resource
  • document is not defined in V8
  • Doesn't just interpret JS
    • Parse to AST
    • Ignition: bytecode generation
    • Sparkplug: fast codegen
    • Turbofan: optimized codegen
    • reminder you can define numbers as 1_000_000 in js
    • objects have a Map, a hidden class.
    • what is % in fromnt of functions? It's "Native syntaxes". console.log(%MaxSmi())
    • You can set V8 flages with node using require('v8').setFlags (?) ('--flag')
    • Native syntax: undocumented V8 APIs.
    • Benchmark JS for testing optimizations... optimizations DO matter, up to 2x faster for a simple add function.
    • V8OptimizationStoatus() to get the status of optimization
    • How do debuggers work?
      • Chrome Debug Protocol
        • Has a Heap profiler and CPU profiler
          • CPU profiling is done with polling, similar to the top program (?)
    • See the Torque language which compiles directly into v8 bytecode. torque

Notes by kevin

  • javascript engine

    • multiple engines: v8, spidermonkey, apple jscriptcore
  • v8 executes js in context

    • ecmascript web specs not part of v8
    • chromium embeds v8 and provides DOM apis
    • node embeds v8 and provides system apis
    • ruby racer embeds v8 to call js from ruby
  • v8 does not just interpret js

    • many parts
      • parse, optimization, codegen
      • turbofan: part that optimize code
  • optimizing

    • what v8 mainly optimizes
      • inlinable functions
      • function with stable signature
  • functions example

  • objects example

    • getObject 1e6 times
    • to optimize object, requires
      • same fields with same type
      • types crated in same order
    • calling same object initialized with object of different order will stop optimizations
  • native funciton syntax %

    • run v8 with --allow-native-syntax

      TIP: you can set v8 flags inside nodejs

    • native syntax are undocumented v8 apis, used for testing and debugging
      • you can find the v8 source
      • docs are in v8 tests
    • can used to manipulate optimization (eg. never optimize this function)
  • what can i do with native functions?

  • question1: are optimized functions faster?

    • benchmarkjs to test
  • question2: check optimization status of a function within code

    • print to debug
    • if you keep opt/de-opt function, v8 will give up on your code
  • question3: can i manipulate memory related stuff

    • yes, but please do it
  • how does devtools work

    • chrome debug protocol
      • websockets
      • communicate with v8/chromium
      • access debug interface:
        • debugger
        • heap profiler
        • cpu profiling/code coverage
        • dom (browser only)
        • eg. playwright uses this
        • eg. remote debug node
      • gives access to everything in node process
  • debug protocol

    • cpu profiling is polling based
    • control polling rate from protocol (not available in dev tools)
  • heap profiler

    • show everything in heap
    • eg. find memory leak
  • why

    • remote debugging
    • atuomated diagnostics
    • script debugging
      • eg. add breakpoint
      • execute code and check heap
      • canbe used in extensions
  • other topics

    • v8 inlining
    • torque language
    • v8 treats web assembly
    • how v8 manages strings
    • etc
    • most of this is on https://v8.dev/

Notes by aminamos

  • v8 is a JS engine
    • the engine takes JS code as an input and executes it
  • v8 is one of many engines
  • DOM manipulation APIs are not defined in v8
  • node embeds v8 and has JS APIs
  • v8 is embedded (ruby, deno examples)
  • turbofan, sparkplug, ignition
  • [discussion around code optimiziation, de-optimization, re-optimization]
  • are optimized functions better? yes
  • can I check optimization status of a function in the code? yes
  • chrome debug protocol convo, CPU profiling is polling based (and this rate is configurable)
  • can get heap dump, then dig into info in dev tools (all objects, all streams that are currently alive)
  • other toppics
    • v8 code inilining, WebAssembly, string management, torque language, v8 snapshots
    • check out https://v8.dev/


Children
  1. Hunt
  2. Kevinslin

Tags

  1. v8
  2. memory-leaks
  3. profiling
  4. shadow-classes
  5. chrome
  6. node-js
  7. deno

Backlinks