This article has participated in the third “topic writing” track of the Denver Creators Training Camp. For details, check out: Digg Project | Creators Training Camp third is ongoing, “write” to make a personal impact

We all know that JavaScript is parsed and run by the V8 engine, but how does V8 work? How does it work? So let’s analyze it together

In-memory data storage

First let’s take a look at JavaScript variables and how data is stored. Why do we declare a variable and then call it elsewhere? Where do we put the declared variable?

  • Stack space

    Stack space is where the context of the call stack is stored.

  • Heap space

    Heap space is used to store reference type data.

    Why is there heap space? Because stack space needs to be released and added frequently. A quick response is required. The size of stack space is small, and a lot of data is large. So we have heap space. Heap space data is in stack space with a reference address corresponding to the heap space. Access by reference address when we access. So when we copy the reference type data. Just copy the reference address, reference the specific data is the same one.

  • Code space

    Store our executable code.

  • Deep copy

    Because reference type assignments cannot be copied, they do not affect the purpose of the original object.

    let obj1 ={
        a:1,
        B:2,
        C:3
    }
    let objString = JSON.stringify(obj1)
    let obj2 = JSON.parse(objString)
    obj2.a = 5 
    console.log(obj1.a)   //1
    console.log(obj2.a)   //5
    Copy the code

    Analyze:

    Obj1 stores a reference address in stack space. ObjString is a string, stored in the stack space, initially undefined, obj is a read variable, read from the stack space by reference to the value of the heap space, By converting it to a string and assigning it to objString,objString is already a base variable in the stack space. Obj2 is then assigned to parse base data objString, and a new reference address is obtained. So you can achieve the purpose of deep copy.

Garbage collection mechanism

Language garbage collection can be divided into automatic and manual garbage collection. Js is the automatic recycle mechanism.

The collection of the call stack determines the execution state of the current call through a pointer, and the move down operation of the pointer destroys the execution context of the function.

Heap recycling:

Js recycling mechanism is divided into three steps: marking – cleaning – sorting

Js divides the heap into new generation and old generation. Cenozoic is data with small storage and short survival time. Old generation is large storage. Data with long survival time.

  • Application of the Scavenge Avenge

    The recycling mechanism is to divide the new generation into two halves, the object region and the idle region. When the object area is nearly full, the collection begins, first marking the garbage. The surviving object data is then sequentially copied to the free region. Interchangeably object regions and free regions. Achieve recycling.

  • The old generation uses the main garbage collector

    Approximately the process of mark – clean – tidy.

How does V8 execute code

Let’s start with a few conceptual words: compiler, interpreter, Abstract syntax tree (AST), Bytecode, Just-in-time compiler (JIT)

Compilers and interpreters are distinguished by the flow of language execution.

  • Compiler source – AST – intermediate code – binaries – execution. The first time the full step is executed, the second time it might be read directly from the binary.
  • Interpreter source code — AST — bytecode — execution. Each execution requires an interpreter to interpret the language to run.

An abstract syntax tree (AST) is an abstract representation of code that is one step closer to the target.

How is V8 implemented?

The browser takes the source code, creates the runtime environment, extracts the context, converts the executable code into an AST tree, which is a data structure that the interpreter can recognize, takes the ast tree, converts it into bytecode, and executes it. Normal is such a process. From source code to computer execution.

Just-in-time compiler

When the interpreter collects information about the code in parallel during the execution of the interpretation and finds a part of the code active. This code is given to the just-in-time compiler, which converts it to binary and saves it. Next time I’ll use direct read.

Reference article:

How the browser works -V8