Front-end tools and frameworks update themselves at a snail’s pace, and to keep up, you need to grasp the essential knowledge that makes it easier to understand the top applications.

Compiler and interpreter

Because the machine cannot understand high-level code directly, it needs to be translated into machine code by a compiler or parser

Compiler

Compiled languages require the compiler to compile the program before it can be executed, and after the compilation, the machine-readable binary is kept so that it can be run each time the program is run without having to recompile it. C/C++, GO, etc are compiled languages.

Interpreter

A program written in an interpreted language needs to be dynamically interpreted and executed by an interpreter each time it is run. Python, JavaScript, etc., are interpreted languages.

V8 executes a JavaScript code flow

  1. Generate abstract syntax tree (AST) and execution context

    A high-level language is one that developers can understand, but one that compilers or interpreters can’t. For a compiler or interpreter, the AST is all they can understand. So whether you’re using an interpreted language or a compiled language, they all generate an AST during compilation. This is similar to how a rendering engine converts an HTML-formatted file into a DOM tree that a computer can understand.

    • The first stage is tokenize, also known as lexical analysis, which breaks down the lines of source code into tokens.
    • The second stage is parse, also known as syntax analysis, which converts the token data generated in the previous step into the AST according to the syntax rules.
  2. Translate code into Bytecode

    Since it is efficient to execute machine code directly, running on a phone with 512 MEgabytes of memory exposes the memory footprint, as V8 consumes a large amount of memory to store the converted machine code. To address the memory footprint, the V8 team dramatically reconfigured the engine architecture, introduced bytecode, and ditched the previous compiler,

    Bytecode is a type of code between AST and machine code. But regardless of a particular type of machine code, bytecode needs to be translated into machine code by the interpreter before it can be executed.

    Machine code takes up much more space than bytecode, so using bytecode can reduce the memory usage of the system.

  3. Execute code just-in-time Compiler (JIT)

    In Ignition, if you find a HotSpot — for example, a piece of code that has been executed multiple times — and TurboFan, the compiler in the background, compiles that bytecode into efficient machine code, When the optimized code is executed again, it only needs to execute the compiled machine code, which greatly improves the efficiency of the code execution.

    Java and Python virtual machines are also implemented on a just-in-time (JIT) basis (bytecode with interpreter and compiler). In V8, the interpreter Ignition collects code information while interpreting the bytecode execution, and when it sees a section of code getting hot, the TurboFan compiler converts the hot bytecode to machine code and saves it for future use.

    V8 uses “bytecode +JIT” technology in addition to Apple’s SquirrelFish Extreme and Mozilla’s

    SpiderMonkey also uses this technique.

reference