Open In App

What happens inside JavaScript Engine ?

Improve
Improve
Like Article
Like
Save
Share
Report

JavaScript is a multi-paradigm prototype-based language, which uses JavaScript Engine such as Chrome’s V8 engine Firefox SpiderMonkey engine and etc. They convert the high level code into machine-readable code which lets computer to perform some specific tasks. We will understand this using an image.
 

Google chrome’s JavaScript V8 engine: Firstly, raw JavaScript file goes into the Parser

  • Parser: It checks for syntax and semantics. Parser is nothing but a lexical analysis that results into the breaking of code into tokens in order to understand their meanings and these tokens gets converted into Abstract Syntax Tree(AST).
  • Abstract Syntax tree: It is a hierarchical tree like structure of program representation which allows interpreter to understand the program. This AST is initially goes to the Interpreter.
  • Interpreter: It lets the AST to get converted into Byte code. In V8 engine, this process is known as Ignition but when some code gets repeated again and again.
    For example: 

javascript




// Arrow function
const multiply = (a, b)=> a*b;
 
for(let i=0;i<1000;i++){
    console.log(multiply(4, 3));
}


  • In the above code, we are calling multiply() function 1000 times. When this code goes into the interpreter, the interpreter performance got decreased, since, Interpreter had to repeat this code again and again and then profiler will mark this code as warm and comes into action.
  • Profiler: It will check for the repeating code that can be optimized. As soon as, it gets the repeating code, it basically moves the code into compiler.
  • Compiler: It spits out the most optimized byte code. In the above case, it will see the repeating code and optimize the code by replacing the multiply(4, 3) as 12, since it gets repeated again and again and it will produce the optimized byte-code which gets replaced with the slower byte code produced by the Interpreter. In V8 Engine, This compiler is called as TurboFan. This process gets repeated again and again which means that JavaScript Engine’s speed gets improved since profiler and compiler will be producing and updating the optimized byte code.

Mozilla’s SpiderMonkey JavaScript Engine: SpiderMonkey is the first Engine created by Brendan Eich, Creator of JavaScript. He created this Engine at Netscape Communication in 1995 and now it is maintained by Mozilla Foundation. We will understand this using an image. 

The Spider Monkey converts the main JS code into the byte code through the compiler, after that the byte code goes into two section Interpreter and JIT Compiler.
Mozilla’s SpiderMonkey Engine three things are important which are as follows: 

  • Interpreter: It is used a switch statement to jump to the appropriate chunk of code for the current instruction. The JS-to-JS function call pushes a JavaScript stack frame without growing the C stack. But since JS-to-C-to-JS call stacks are common, the interpreter is re-entrant.
  • IonMonkey JIT Compiler: It is mainly used for optimization of code.
  • Garbage collector: It is used for claiming the memory used by objects that are no longer used by the program. The GC is a mark-and-sweep, non-conservative collector. It is used to hold the JS objects and the string descriptors.

Difference between Chrome V8 vs Mozilla’s Spider Monkey: The main difference between chrome’s V8 Engine and Mozilla’s SpiderMonkey lies in ECMAScript conformance Test262 which is used to check how closely JavaScript implementation follows the ECMAScript 5th edition specification standards, created by ECMA International. This test consists of thousands of individual tests, which checks the requirement of the specification.
Conformance test: 

Chrome’s V8 Engine -> 98%

Mozilla’s SpiderMonkey -> 87%


Last Updated : 25 Nov, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads