You are currently viewing What’s Inside JavaScript Engine?

What’s Inside JavaScript Engine?

  • Post published:March 10, 2021

If you’re looking to know the underlying mechanics of Javascript you need to know how the Javascript engine works.

It’s essential you understand how the Javascript engine works because it allows one to have a firm understanding of the language itself. Digging deeper into Javascript requires us to understand what composes the v8 Javascript engine and how it works. In fact, if you have a firm grip over Javascript engine you’ll be able to write better code and produce efficient apps.

how the JavaScript engine works

How the JavaScript Engine Works?

A Javascript engine can simply be stated as a program or an interpreter that carries out code execution. The process of compilation from Javascript to bytecode is carried out through Javascript engine that can be seen as a standard interpreter or just-in-time compiler.

The most popular is the V8 Javascript engine which is used in Chrome and Node.js and is developed by Google. There are many other Javascript engines as well that include SpiderMonkey, Rhino, and JavaScriptCore.

The Purpose of V8 JavaScript Engine

The purpose of V8 Javascript engine was to increase the performance of JavaScript execution inside web browsers. By performance we simply imply an increase in speed and the v8 carries this out by translating Javascript code into efficient machine code instead of using an interpreter. This entire process is implemented through the JIT or Just-In-Time compiler at execution. This flow of execution is carried out through all Javascript engines but the only difference lies in V8 is that it doesn’t produce bytecode or any form of intermediate code.

V8 Engine & Threads

The main thread in JavaScript has the function to fetch, compile and execute code. Besides, the main thread there is another thread that is solely for compiling and they both are run together. The main thread keeps on compiling the code but the compiler thread optimizes the code for better performance. A profiler thread also joins in that shows the runtime on the methods so that Crankshaft can better optimize them. Some Garbage Collector Threads are present as well.

V8 Engine & Threads

The First Execution

The first time Javascript executes code, the V8 Engine uses full-codegen which translates the parsed Javascript into machine code without transformation. This basically means that Javascript executes machine codes at a very fast rate. Furthermore, if we talk about v8 engine itself it does not use an intermediate bytecode representation which means that an interpreter is not needed. This further speeds up the process of code execution.

After the first execution the profiler thread comes into play again. When the code has received some run time, the profiler thread gathers all the data and also monitors and optimizes the code.

Moving on, the Crankshaft optimization takes over in a separate thread. The JS abstract syntax tree is translated into a static single-assignment (SSA) called Hydrogen. Furthermore, optimization for Hydrogen graphs is carried out.

In a Nutshell

Essentially, a Javascript engine is a program that focuses on Javascript code. In the beginning, the Javascript engines were there for just interpretation but now things have changed as the engine uses just-in-time compilation. This put focus towards more improved performance.