The engine contains a few parts: parser, AST, Interpreter, profiler, and compiler.
Parser: Does lexical analysis which breaks code to tokens to identify their meanings.
AST (Abstract Syntax Tree): The tokens are formed into a tree to work with. there is an online tool that demonstrates this, check it out here.
In programming, there are 2 ways to translate to machine language: Interpreting and compiling.
Interpreter: translates and reads the files line by line on the fly. easy to get up and running but it’s slow.
Profiler: Monitor and watch the code as it runs and makes notes of how the code can be optimized (how many times a specific code runs, what types are used).
Compiler: Works ahead of time to create a translation and compiles it down to a language that the machine understands. the start takes a bit of time but code run faster
With the compiler, it takes a bit of time to start because of the compilation step at the beginning but it’s smart enough to improve code to be faster before execution. it makes an optimization, it returns an optimized code.
Now that you know how the engine works, you can:
- Make sure that you don’t confuse the compiler because the compiler can make mistakes and try to optimize code that it can’t. in that scenario, it will do something called deoptimization which will take longer time to revert it back to the interpreter.
The 2 main things that the engine does for us are reading the code and executing it. it means that we need a:
- place to store and write information. to store variables objects and such.
- place to run and keep track of what’s happening line by line on the code.
for these 2 we use the “call stack” and “memory heap”.
Memory Heap: Place to store and write information. like allocate memory, use memory and release memory.
Call stack: Place to keep track of where we are in the code to run the code in order.
The issue with single-threaded is that when a function takes a long time to complete it will freeze the web page and the user won’t be able to interact with the page until the call stack is empty.