NodeJS V8 Engine


What is V8?

V8 is a JavaScript engine build in the google development center in Germany. It is open source and written in C++. It is used for both client side (Google Chrome) and server side (node.js) JavaScript applications.

V8 was first designed to increase the performance of the JavaScript execution inside web browsers. In order to obtain speed, V8 translates JavaScript code into 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 such as SpiderMonkey or Rhino (Mozilla) are doing. The main difference with V8 is that it doesn’t produce bytecode or any intermediate code.

The aim of this article is to show and to understand how V8 works, in order to produce optimized code for both client side or server side applications. If you are already asking yourself “Should I care about JavaScript performance?” then I will answer with a citation, from Daniel Clifford (tech lead and manager of the V8 team): “It’s not just about making your current application run faster, it’s about enabling things that you have never been able to do in the past”.

How V8 compiles JavaScript code?


V8 has two compilers!


  • A “Full” Compiler that can generate good code for any JavaScript: good but not great JIT code. The goal of this compiler is to generates code quickly. To achieve its goal, it doesn’t do any type analysis and doesn't know anything about types. Instead, it uses an Inline Caches or “IC” strategy to refine knowledge about types while the program run. IC is very efficient and bring about 20 times speed improvment.
  • An Optimizing Compiler that produces great code for most of the JavaScript language. It comes later and re-compiles hot functions. The optimizing compiler takes types from the Inline Cache and make decisions about how to optimize the code better. However, some language features are not supported yet like try/catch block for instance. (The workaround for try/catch blocks is to write the “non stable” code into a function and to call the function in the try block)

How V8 optimized JavaScript code

 Tagged values


To have an efficient representation of numbers and JavaScript objects, V8 represents both of us with a 32 bits value. It uses a bit to know if it is an object (flag = 1) or an integer (flag = 0) called here SMall Integer or SMI because of its 31 bits. Then, if a numeric value is bigger than 31 bits, V8 will box the number, turning it to a double and creating a new object to put the number inside.

Code optimization

Try to use 31 bit signed numbers whenever possible to avoid the expensive boxing operation into JavaScript object.

 Arrays


V8 uses to differents methods to handle arrays: * Fast elements: Designed for arrays where 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 elements inside of them. It is actually a hash table, more expensive to access than “Fast Elements”

Code optimization 

Be sure that V8 uses “Fast Elements” to handle arrays, in other words, avoid sparse arrays where keys are not next incremental numbers. Also, try to avoid to pre-allocate large arrays, better is to grow as you go. Finally, don’t delete elements in arrays: it makes the key set sparse.

Comments

Popular posts from this blog

CSRF Token-Synchronizer Token Patterns

CSRF token - Double Submit Cookies Pattern

Cloud Computing