What is the V8 JavaScript Engine in the Nodejs?

A V8 JavaScript engine of Nodejs is an interpreter that executes JavaScript code. It can be used as a Standard interpreter or Just-In-Time(JIT) compiler that compiles JavaScript to some form of byte code. 

Here, we have listed some popular projects that are implementing JavaScript engine:

  • V8 – An open-source written in C++, developed by Google.
  • Rhino –  Developed by Mozilla Foundation in Java, which is completely open-source.
  • SpiderMonkey – Considered to be the first-ever Javascript. Backed Netscape Navigator then, currently powering Firefox.
  • JavaScriptCore –  Originally developed by Apple for Safari, an open-source marketed as Nitro.
  • Chakra – A JScript9 engine developed for Internet Explorer.
  • Chakra – A JavaScript engine developed for Microsoft Edge.
  • Nashorn – A open-source written by Oracle Java Languages and Tool Group as part of OpenJDK.
  • JerryScript – A simple, lightweight yet powerful engine for the Internet of Things.

V8 JavaScript engine IN NodeJS

v8-engine-javascript-runtime-for-node-js

The V8 Engine is an open-source developed by Google and written in C++. V8 Javascript engine is for the popular Nodejs runtime, unlike other engines, it fetches inside the Google Chrome browser.

V8 was originally designed to increase the performance of overall JavaScript execution inside web browsers. It translates JavaScript code into an efficient machine code instead of using an interpreter to obtain speed. Like other modern JS engines, V8 compiles JS code into machine code with a JIT (Just-In-Time) compiler. Some popular projects that implemented JavaScript engines are SpiderMonkey, Rhino, Jerry script, etc.

Read: What is event emitter in Node.js?

The key way that the V8 engine differs from the other engines is, it doesn’t produce byte code or any intermediate code.

Two compilers of V8 ENGINE

The V8 engine used two compilers to compile JavaScript code,

  • Full-Codegen — A simple and fast compiler that produces low level and slow machine code.
  • Crankshaft — A Just-In-Time complex optimizer come compiler that produces a highly-optimized code.

The V8 Engine also uses several threads internally:

  • The main thread of the Nodejs V8 JavaScript engine will do almost everything that a compiler can do. And also fetches your code, compiles it, and then executes it.
  • A Profiler thread calls the runtime, when a time-consuming method on execution, so that Crankshaft can optimize them.
  • Here some threads are allocated to handle Garbage Collector.

Key components of the V8 engine

V8 javascript engine

There are some key components of the V8 Javascript engine that ensure the high performance in Nodejs applications. Also, the clean execution of the JavaScript code for better compilation and other operations.

  • JS interpreter – Ignition and Optimization compiler
  • Garbage collection – Old generation sweeper
  • Web Assembly – Liftoff

JS interpreter

V8 achieved such speed from the Just-In-time (JIT) compilation of JavaScript. It complies JS to the native machine code, just before executing it. The process flow involves the following: A baseline compiler compiles the code, which quickly generates non-optimized machine code. On runtime, the compiled code is analyzed and can be re-compiled for optimal performance. Ignition provides the Turbofan at first and Crankshaft in the second.

JIT compilation results in occupying a large amount of memory to execute machine code, which even may take a while to execute. Whereas Ignition comes as the solution that executes code with less memory overhead. 

The Turbofan project started in 2013 to fix the shortcoming of Crankshaft which fails to optimize some part of the JavaScript functionality (i.e. Error handling). In general, it was designed to optimize both the existing and upcoming features at the time.

Garbage collection

V8 uses one of the traditional generational approach of mark-sweep to clean the old generation. This Garbage collection consists of two major phases: Marking and Sweeping. In order to make a stable execution, the marking phase stops the JavaScript execution. V8 Javascript uses an incremental marking for the effective collection of garbage in the Nodejs apps. Instead of walking the whole heap, it walks only a part of the heap, then resumes normal execution.

Web Assembly

Web Assembly in the Nodejs V8 JavaScript engine is to achieve great performance. It is a key task for any browser to generate a machine is to the task Liftoff. Unlike other complex generators, Liftoff is a simpler code generator, which generates code for each opcode at a time. It generates machine code much faster than Turbofan even to x10 range.

CONCLUSION

Nodejs V8 JavaScript engine will greatly help if you build an extensive Nodejs web application. If you’re planning to build a scalable web application, Contact us for more information flow.

Leave a Reply

Your email address will not be published. Required fields are marked *

Facebook
Twitter
INSTAGRAM
LinkedIn