JS Engine Exposed โ€“ Google's V8 Architecture (Made Simple!)


What Makes JS Run? โ€” The JavaScript Runtime Environment (JRE)

JS needs a special environment to run โ€” this is called the JavaScript Runtime Environment (JRE).

Think of JRE as a Kitchen:

  • JS Engine: The chef who cooks your code.

  • Web APIs: Extra tools like microwave, oven, or fridge (things like DOM, setTimeout, etc.).

  • Event Loop: The kitchen manager who decides when things get cooked.

  • Callback & Microtask Queue: Waiting lines for dishes to be cooked.

๐Ÿ’ก Important: Browsers (like Chrome, Firefox) and some platforms (like Node.js) provide this environment so that your JS code can work.


๐Ÿ“œ ECMAScript & JavaScript Engines

  • ECMAScript: It's like a rulebook (standard) that every JS engine follows.

  • JavaScript Engine: Software that understands and runs your JS code.

Different browsers use different JS engines:

  • Chrome: V8

  • Firefox: SpiderMonkey

  • Safari: JavaScriptCore

  • Edge: Now uses V8 (used to have Chakra)


What is a JavaScript Engine?

A JS Engine is not a machine. Itโ€™s a program written in low-level languages like C++. Its job is to take your JS code and turn it into something your computer understands โ€” machine code.

Letโ€™s understand what happens inside.


๐Ÿ”„ The 3 Stages of Code Inside JS Engine

When you write JavaScript like:

let a = 5;

The engine processes this in 3 steps:

1. Parsing:

  • Breaks code into tokens (small parts): let, a, =, 5.

  • Creates an AST (Abstract Syntax Tree) โ€“ a tree structure that represents your code.

You can try this yourself at astexplorer.net


2. Compilation (Yes, JS Compiles!)

JavaScript uses something called JIT (Just-In-Time) Compilation, which is a mix of:

  • Interpreter: Quickly runs the code.

  • Compiler: Optimizes it while it runs.

โœ… So yes, JavaScript is compiled! It's not just interpreted like in the old days.

๐Ÿ‘‰ The engine compiles and executes at the same time to improve speed.

Example:

function add(a, b) {
  return a + b;
}
add(2, 3);
  • First, interpreter runs it.

  • If the function is used often, compiler will optimize it on the go!

Extra Reading:


3. Execution:

Once the code is compiled, it's ready to run.

Two important parts during execution:

  • Memory Heap: Where all variables and objects live.

  • Call Stack: Keeps track of function calls (Remember this from async JS?).

๐Ÿ‘ฎโ€โ™‚๏ธ Garbage Collector: Cleans up unused memory using a method called Mark and Sweep (marks unused variables and removes them).


V8 Engine โ€“ Behind the Scenes

The V8 Engine (used in Chrome & Node.js) has:

  • Ignition โ€“ Interpreter

  • TurboFan โ€“ Optimizing Compiler

  • Orinoco โ€“ Garbage Collector

This combo helps JS run fast and efficiently.


Want to See This Live?

Watch this full explanation on YouTube by Akshay Saini:

๐Ÿ”— JS Engine Exposed โ€“ Akshay Saini


๐Ÿ“ Final Notes

  • JavaScript Engines have evolved a lot โ€” from simple interpreters to smart compilers.

  • JS is now a JIT compiled language (Best of both worlds).

  • Knowing how your code runs helps you write better and faster applications.


0
Subscribe to my newsletter

Read articles from UR Prakash Gupta directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

UR Prakash Gupta
UR Prakash Gupta

My name is ๐”๐‘ ๐๐ซ๐š๐ค๐š๐ฌ๐ก ๐†๐ฎ๐ฉ๐ญ๐š and I talk about ๐—ง๐—ฒ๐—ฐ๐—ต-๐Š๐ง๐จ๐ฐ๐ฅ๐ž๐๐ ๐ž, ๐—ช๐—ฒ๐—ฏ๐——๐—ฒ๐˜ƒ, ๐——๐—ฒ๐˜ƒ๐—ข๐—ฝ๐˜€ and ๐—Ÿ๐—ถ๐—ณ๐—ฒ๐˜€๐˜๐˜†๐—น๐—ฒ.