The V8 Engine Logo
Googles V8 Engine

Inside the V8 Engine

Cameron J. Leverett
Nerd For Tech
Published in
3 min readMar 25, 2021

--

Definitions

  • (AST) or “Abstract Syntax Tree” — is a way of representing the syntax of a programming language as a hierarchical tree-like structure
  • profiling (“program profiling,” “software profiling”) is a form of a dynamic program that measures, for example, the space (memory) or time complexity for a program, the instructions or the frequency and duration of function calls
  • A compiler is a unique program that processes statements written in a particular programming language and turns them into machine language or “code” that a computer’s processor uses.
  • Lexical — closely matching the meaning of the term in common usage. As its other name implies, this is the sort of definition one is likely to find in the dictionary.

To those who don’t know what the v8 Engine is, it is essentially the engine that interprets JavaScript or compiles it. It’s important to note that the v8 engine is not the only engine out there. The V8 Engine is just the one that Google and NodeJS happen to use. To see more about what browsers use which engine, click here.

There Five basic steps of the V8 Engine.

  1. Parser
  2. AST
  3. Interpreter
  4. Profiler
  5. Compiler

When we run a JavaScript file, the code is broken down into tokens; you’ve probably heard the terms lexical analysis, lexing, or Tokenizer. Lexical analysis is just the process of breaking down code into tokens based on the meaning of the keyword or what the code is trying to do. These “tokens,” as we call them, are then formed into an AST, and there is a fun tool that allows you to view the AST if you’d like to check it out.

Once the AST has finished, the code will be pushed to the interpreter and thrown into a compiler or profiler. However, the V8 Engine uses JIT (Just-In-Time) compiler. The JIT-compiler actually isn’t a compiler at all or an interpreter. It can be thought of more like a hybrid between the both. The JIT compiler allows us to execute code at run time instead of executing it before the code runs. With JIT, we get heavy optimization that enables the compiler and profiler to optimize the byte code better to work more efficiently. It's important to note that Byte code is not Machine code. Byte code needs to operate through v8 Engine to execute it directly by the virtual machine or a JS engine.

A common misconception is that JavaScript is an interpreted language. You have probably heard something like, “JavaScript is an interpreted language,” but this isn’t always the case. You could create an implementation of JavaScript using the JS Engine that only compiles if you wanted to. I’m not saying that it would have any optimized impact; it's just a fun fact. What it can do is help you understand JavaScript on a deeper level.

--

--

Cameron J. Leverett
Nerd For Tech

I love tackling new and exciting challenges and working with software because it’s consistently changing, which is exciting.