Quick Overview an V8 Engine How does actually work?

Quick Overview an V8 Engine How does actually work?

Β·

3 min read

Hello, Learners πŸ‘‹πŸ‘‹

Welcome to reached here we are going to learn in a short way engine first thing very clear here is we are not talking about instrumental engine πŸš‚ might be you got startled πŸ™„ but it's reality.

So what does that engine this question is running in your head let's try to understand it in the point of view of a javascript language engine means something written code inside your system so that's exactly let's must deep down it and explore it.

What is JavaScript Engine?

A JavaScript engine is a computer program that executes the JavaScript code you write. It translates your code into what computers understand. There are many JavaScript Engines out there and typically they are created by web browser vendors. All engines are standardized by ECMA Script or ES.

A brief history about JavaScript Engines

The first JavaScript engine was written by Brendan Eich, the creator of JavaScript, in 1995 for the Netscape Navigator web browser. Originally, the JavaScript engine only consisted of an interpreter. This later evolved into the SpiderMonkey engine, still used by the Firefox browser.

In 2008, Google created the Chrome V8 Engine. The V8 engine is an open-source high-performance JavaScript engine, written in C++ and used in the Chrome browser as well as in Node JS. The performance outmatched any engine that came before it mainly because it combines 2 parts of the engine, the interpreter and the compiler. Today, all major engines use this same technique.

Working of V8 Engine

V8 Inside.png

  1. Parsing Phase Parsing is the process of analyzing the source code, checking it for errors, and breaking it up into parts.

  2. AST Conversion The parser produces a data structure called the Abstract Syntax Tree or AST. AST is a tree graph of the source code. It does not show every detail of the original syntax but contains structural details.

  3. Interpreter An interpreter executes each line of code line by line, without requiring them to be compiled into a machine language program. Interpreters can use different strategies to increase performance. They can parse the source code and execute it immediately, translate it into more efficient machine code, execute precompiled code made by a compiler, or some combination of these. In the V8 engine, the interpreter outputs bytecode.

  4. Profiler In modern engines, the interpreter starts reading the code line by line while the profiler watches for frequently used code and flags then passes it to the compiler to be optimized.

  5. Compilation Phase The compiler works ahead of time to convert instructions into a machine code or lower-level form so that they can be executed by a computer. It runs all of the code and tries to figure out what the code does and then compiles it down into another language that is easier for the computer to read.

  6. A JavaScript Engine is an interpreter that interprets JavaScript code and runs it. The first way to develop a JavaScript engine is to implement it as a standard interpreter, as done by Mozilla’s SpiderMonkey. The other option is to employ Just-in-Time (JIT) compilation, which turns native JavaScript code into machine code as V8 does. The distinction between V8 code and other programming languages is that it does not generate intermediate code.

    The Ignition interpreter compiles JavaScript code and generates non-optimized machine code when a developer or program runs it on V8 (i.e. in a browser or Node environment). The Turbofan and Crankshaft components of V8 examine and recompile the machine code at runtime for optimal performance.

workinggfg.png

Conclusion

First, JavaScript code is parsed and converted to an AST. Secondly, it is interpreted. Then the profile watches and stores are used to code and flags. Then JavaScript engine takes the bytecode of the interpreter outputs and mixes in the optimized code the compiler outputs and then gives that to the computer. This is called Just in Time or JIT Compiler.

Β