How does the V8 JavaScript Engine Work?

As we know, JavaScript is getting popular more and more, teams are leveraging its support on many levels in their back-end hybrid app and much more.

This article will try to find out what JavaScript is and how it works. JavaScript is a widely used programming language which is one of the 3 core technologies of the World Wide Web, the other two being HTML and CSS. It is used for developing websites of all kinds, including ecommerce websites . By understanding the building blocks of JavaScript and how they come into play together, you will be easily able to write better codes and apps. Writing code for the web sometimes feels a little magical in that often we write a sequence of characters and things happen in a browser. But understanding the technology behind the magic can help you sharpen your craft as a programmer. Any JavaScript developer might be very curious about the engines that underlie this technology that those we often use on a daily basis.

So let’s understand what a JavaScript engine does, why different platforms use different engines?

A JavaScript engine  has often been termed as a kind of virtual machine. A virtual machine refers to the software-driven emulation of the given computer system. There are so many types of virtual machines and they are able to emulate actual physical machines. A system virtual machine provides a complete emulation of the platform on which an operating system can be executed. Mac users are familiar with parallels in which a system virtual machine allows you to run windows on your Mac.

On the other end, a process virtual machine is less full-functional and can run one program. Wine is a process virtual machine that gives permission to you to run Windows applications on Linux machines but does not provide an entire Windows OS on a Linux box. In some cases, if projects are getting so much dependent on JavaScript, this means that JavaScript developers have to be utilizing everything that the language and the ecosystem provide with deeper and deeper understanding of the internals, in order to build good software.

What exactly a JavaScript engine Does

The most basic job of a JavaScript engine, when all is said and done, is to take the JavaScript code that a developer writes and convert it into fast, optimized code that can be interpreted by a browser.

Each JavaScript engine applies a version of ECMAScript, of which JavaScript is a dialect. Given the definition of a virtual machine above, it makes sense to term JavaScript engine a process virtual machine. Although it’s primary purpose is to read and compile JavaScript code. This really doesn’t mean that it is a simple engine.

JavaScript Core performs a series of steps to interpret and optimize a script:
1. It performs a lexical analysis, breaking down the source into a series of tokens.
2. Four JIT processes then click on, analyzing and executing the bytecode produced by the parser.

How Google’s V8 JavaScript Engine Works?

Google’s V8 engine, written in C++, also compiles and executes JavaScript source code, handles memory allocation and collects garbage. This is the list of popular projects that are implementing a JavaScript engine:
1. V8- open source, developed by Google and written in C++.
2. JavaScript Core- open source, marketed as Nitro and developed by Apple for Safari

The V8 design consists of two compilers that compile source code directly into machine code.
•  Full-codegen:  It is a fast compiler that produces unoptimized code
•  Crankshaft: a slower compiler that produces fast and optimized code

If crankshaft evaluates that the unoptimized code generated by Full-codegen is in need of optimization, it replaces it, a process known as crank shafts. The V8 engine is used inside Google Chrome. Unlike the other engines, V8 is also used for the popular Node.js runtime. It was first designed to increase the performance of JavaScript execution inside web browsers. To increase the speed, V8 translates JavaScript code into a more efficient machine code instead of using an interpreter. It compiles JavaScript code into machine code at execution by implementing a JIT compiler like a lot of modern JavaScript engines like Rhino, and JavaScript engines do.

The V8 engine also uses several threads internally:
•  The main thread does what you would expect, fetch your code, compile it and at last execute it.
•  A separate thread is also used for compiling, so that the main thread can keep executing while the former is optimizing the code.
•  A profiler thread that will tell the runtime on which methods we spend a lot of time so that crankshaft can optimize them.
•  Few threads to handle garbage collector sweeps.

Inlining
The very first optimization is inlining as much code as possible in advance. Inlining is the process of replacing a call sit with the body of the called function. This simple step allows following optimizations to be meaningful. This optimization removes boxing/unboxing operations.

Conclusion
Once the Hydrogen Graph is optimized, Crankshaft lowers it to lower-level representations called Lithium. Finally, lithium is compiled into machine code. Then On Stack replacement happens. We are about to transform all the context we have so that we can switch to the optimized version in the middle of execution.

Write a comment
Cancel Reply
  • Sunny Chawla April 15, 2019, 5:12 am
    Hey, lovely post, I am new in Java Development and this was an informative article for me. Thank You very much…!!!!!!
    reply