Javascript Engine

Brijesh Yadav
2 min readMay 24, 2021

When we want work to be done by the machine, so how do we tell the machine to do that work.

for example, I want to add 2 numbers, so I will write a javascript function to add 2 numbers and ask the machine to run it and give us the result but the machine does not understand anything except Machine Language.

So here we need something so that we can convert our javascript code in machine language and the machine will understand that and can do the work for us.

Js Engine for V8 Engine

The first step is Lexical analysis which breaks the code into tokens to identify their meaning so that he can identify what to do.

And this token is supplied to something called AST (Abstract Syntax Tree) and it forms a tree-like structure of the keywords from javascript.

As you can see in the above image, how js on the left side is converted into a tree structure.

This tree structure created by AST is going into the interpreter and that interpreter will create Byte Code as Shown in the 1st Fig.

Now as you know Byte code is not as Low Level as Machine Code but the javascript engine is able to understand Byte Code and run our program as expected. But this is the first step.

There is something in Fig1 that is a profiler.

Profiler monitors our code as it runs and makes notes on how we can optimize this code.

As code is running then the interpreter tell the profiler that here are some codes to optimize, then the profiler pass the code to the compiler and tries to optimize it that runs faster then replace the older ones with newer optimized code wherever possible

By doing so, the execution speed of javascript will increase.

Now we know how JS Engine works, we can write more optimized code to improve the execution speed of javascript.

Thanks for reading!

--

--

Brijesh Yadav
0 Followers

Passionate about new technologies and love to code in js