During this parsing phase, the code which we write is broken down into Tokens. For example, let a=0; let is one token, a is one token, = is one token, and so on. There is a syntax parser whose job is to take the code and convert it into AST (Abstract Syntax Tree). The resultant AST is passed to the compilation phase.
While on the other hand, many programming languages use the compiler to compile the code. So, in the case of a compiler, the whole code is compiled to its optimized version first, and then it starts executing.
Both have pros and cons. In an interpreter, the code is FAST. It need not wait for the code to be compiled first. It immediately starts executing the code line by line. In the case of compilers, we have EFFICIENCY.
There are two essential components in the execution phase.
- Memory heap
- Call stack
Call stack, whenever a function is invoked, it will be placed in the call stack to execute, and the functions are executed in LIFO (Last In First Out) manner. The event loop will continuously keep checking the call stack, whether it is empty or not. If it is empty and the callback queue has something waiting to be executed, the event loop will push it to the call stack, and this continues until there is nothing left to be executed.
Google V8 Architecture
Google has named its interpreter as Ignition and compiler as TurboFan.
AST is sent to Ignition Interpreter and starts interpreting line by line to convert into Bytecode. Simultaneously, the TurboFan compiler will optimize the code as much as possible and update the Bytecode, which is further passed to the execution phase.
References / Information Sources:
- You don’t know JS (Book Series) by Kyle Simpson