How Javascript engine works?

How javascript is executed in the browser?

Browser does have a javascript runtime environment (eg: v8 engine) that compiles and executes the javascript code.


Meme, Memes, and Dank Memes: HOW
 meme generator net



Basics of understanding Chrome's V8 Engine | by Kevin Duarte | Medium


Where javascript code is converted into tokens, using this tokens syntax parser constructs AST (Abstract syntax tree).


JS is interpretted/compiled ?

JS is both interpretter and compiler.

What is interpretter and compiler ?

interpretter basically takes code and execute it line by line, where in compiler the whole code is compiled first and new optimized version of code is created and then it is executed.

At first JS engine is created only with interpreter because it uses to run on the browser where the end-user can't wait for that code to compile and execute it.

But in today's world, most browsers use both interpreter (Igintion : v8) and compiler (TurboFan : v8) , where AST is passed to the interpreter, when code is interpreted line by line the compiler also will try to optimize the code as much as it can (inlining, inline caching, copy elision). it can happen in multiple phases on runtime.

That's why JS engine compilation is known to be JIT (JUST IN TIME) compilation.


Where bytecode produced by compilation is ready to be shipped to the execution phase, where the execution phase consists of the memory heap and call stack queue.

memory heap is a space where all variables and functions are assigned memory.

Where it has a garbage collector to manage the memory space.

Call stack queue is where the functions declared are executed in a queue.

No comments: