Interesting features of JavaScript V8 engine

Interesting features of JavaScript V8 engine

A JavaScript engine is a program or an interpreter which executes JavaScript code. A JavaScript engine can be implemented as a standard interpreter, or just-in-time compiler that compiles JavaScript to bytecode in some form.

V8 is an open-source JavaScript engine developed by The Chromium Project for Google Chrome and Chromium web browsers. The project’s creator is Lars Bak. The first version of the V8 engine was released at the same time as the first version of Chrome: September 2, 2008. It has also been used in Couchbase, MongoDB, and Node.js that are used server-side. It compiles JavaScript directly to native machine code before executing it, instead of more traditional techniques such as interpreting bytecode or compiling the whole program to machine code and executing it from a filesystem.

Why was the V8 Engine created?

V8 was first designed to increase the performance of JavaScript execution inside web browsers. In order to obtain 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 (Just-In-Time) compiler like a lot of modern JavaScript engines do such as SpiderMonkey or Rhino (Mozilla). The main difference here is that V8 doesn’t produce bytecode or any intermediate code.

The engine used two compilers -

Full-codegen -  a simple and very fast compiler that produced simple and relatively slow machine code.

Crankshaft -  a more complex (Just-In-Time) optimizing compiler that produced highly-optimized code.

When first executing the JavaScript code, V8 leverages full-codegen which directly translates the parsed JavaScript into machine code without any transformation. This allows it to start executing machine code very fast. Remember, V8 does not use intermediate bytecode representation this way removing the need for an interpreter.

V8 uses two different methods to handle arrays -

Fast elements - Designed for arrays where the set of keys are very compact. They have a linear storage buffer that can be accessed very efficiently.

Dictionary elements - Designed for sparse arrays which don’t have every element inside of them. It is actually a hash table, more expensive to access than “Fast Elements”

V8 uses “Fast Elements” to handle arrays, in other words, avoid sparse arrays where keys are not next incremental numbers. Also, try to avoid pre-allocating large arrays. It is better to grow as you go.

 

JavaScript is a prototype-based language. There are no classes and objects are created by using a cloning process. JavaScript is also dynamically typed: types and type information are not explicit and properties can be added to and deleted from objects on the fly.

 

Accessing types and properties effectively makes a first big challenge for V8. Instead of using a dictionary-like data structure for storing object properties and doing a dynamic lookup to resolve the property location, V8 creates hidden classes, at runtime, in order to have an internal representation of the type system and to improve the property access time.

 

V8 creates a new hidden class everytime the constructor function declares a property and keeps track of the change in the hidden class. Why? Because if two objects are created (m and n) and if a member is added to the second object (n) after the creation, V8 needs to keep the last hidden class created (for the first object m) and to create a new one (for the second object n) with the new member.

 

V8 also supports de-optimization: the optimizing compiler makes optimistic assumptions from the Inline Cache about the different types, de-optimization comes if these assumptions are invalid. For example, if a hidden class generated was not the one expected, V8 throws away the optimized code and comes back to the Full Compiler to get types again from the Inline Cache. This process is slow and should be avoided by trying to not change functions after they are optimized.

Comments (0)

  • To add your comment please or

We use cookies to improve your experience on our site and to show you personalised advertising. Please read our cookie policy and privacy policy.

Got It!