# V8 Engine

## About

Written in `C++` by Googgle.

Used to transform Javascript to bytecode so that the machine can execute.

1. `Javascript` -> `C/C++`.
2. `C++` -> `Assembly`.

Used in browsers, nodejs, Deno and others.

## Code Compilation

### Ignition & Turbofan

V8 traditional way is to compile the code in two phases (`Ignition & Turbofan`).

<figure><img src="https://2257067214-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FzCini090qQzVuM4OCasK%2Fuploads%2Fc70XCnH5QFt65pnFg3Nl%2Fnodejs-twophase-compilation.png?alt=media&#x26;token=a21b4bf0-6e40-46be-bbd1-5ccfa4ce3855" alt="" width="563"><figcaption></figcaption></figure>

#### Ignition (Interpreter)

First phase the code is transformed into machine code. This process is **fast** BUT **not optimal**.

*It is enough to get started.*

More detailed steps are:

* V8 parses the source code and turns it into an `Abstract Syntax Tree (AST)`, along with `scopes`.
* Based on the `AST` and `scopes`, the `Ignition` interpreter can produce bytecode.
* At this point, the engine starts running the code and collecting type feedback.

#### Turbofan (Optimizing)

The second phase start at the same time as the first one, but it is a **slower** process as it produces **more optimized** code.

Once the slow compilation finishes, Javascript switches to that optimal compiled code.

* To make it run faster, the byte code can be sent to the optimizing compiler along with feedback data. The optimizing compiler makes certain assumptions based on it and then produces highly-optimized machine code. This happens in parallel and V8 marks frequently used bytecodes as “hot” codes and converts it into more efficient machine code.

### Just-in-Time (JIT)

A newer aproach created for the compilation process.
