Enter Google V8

1. What is V8?

  • Google developed, open source JavaScript engine
  • V8 is also known as the JavaScript virtual machine
  • Function: Executes JavaScript code

2. Two ways in which a computer executes a high-level language

1. Explain execution

  • The input source code is compiled into intermediate code through the parser, and the intermediate code is executed through the interpreter, and the results are input
  • Advantages and disadvantages: Fast startup speed and slow execution speed

2. Compile and execute

  • The source code is converted by a parser into intermediate code, which is compiled by a compiler into machine code (typically a binary file), and finally executed by the machine code
  • Advantages and disadvantages: Slow startup and fast execution

3. V8’s JavaScript execution mechanism

V8 uses a mixture of compilation execution and interpretation execution, known as JIT(Just In Time) just-in-time compilation technology.

4. V8 executes the main flow of JavaScript

  1. Initialize the base environment

    • Initialize the global execution context, built-in functions, event loop systems, and so on
    • Initialized before the code is executed, mainly containing the ones that are needed during execution
  2. Parsing the source generates an AST (abstract syntax tree) and scope

  3. Bytecode generation based on AST and scope

  4. Interprets execution of bytecode (intermediate code)

  5. The interpreter executes the code and monitors the hot code

    • If the interpreter detects that a piece of code is being executed repeatedly, it is marked as hot code
  6. Optimize hotspot code for binary machine code (easy reuse)

    • When V8 finds the hot code, it gives it to the compiler, which compiles the bytecode into binary code
    • V8 also optimizes compiled binaries to make them much more efficient
    • If this code is subsequently executed, V8 will preferentially select the optimized binary for speed
  7. De-tuning generated binary machine code (hot code changes, de-tuning)

    • Once the structure in the hot code object is dynamically modified during execution, the optimized code becomes invalidated
    • Failed hot code needs to be de-optimized, directly through the interpreter, the next time the code is executed


For more study notes and quality content, please pay attention to the public number: Ma Xiaofei learning front end