Deep dive into v8 JS Engine

Now, let's focus on one of Node.js's best friends, the V8 Engine. It takes the code and runs it. Before doing anything, I just want you to read this carefully.
Alright, let’s move on to the session now. When the V8 engine processes the code, it happens in a few steps:
- Tokenizing/Lexing: Your code is broken down into smaller tokens. You can explore this further by visiting AST Explorer. This is more on the academic side, so if you're interested in the details, you can check out the subject of Compiler Design (or just take a look at the screenshot provided). Also, read this article for more insight: What are syntax and expressions in JavaScript.
Interpreted Language vs. Compiled Language
An interpreter reads and executes code line by line, while a compiler reads the entire code, compiles it, and then passes it to the engine for execution. JavaScript uses both a compiler and an interpreter to run the code, along with several optimizations like JIT (Just-in-Time Compiler).
I highly recommend reading Scope and Closures by Kyle Simpson to dive deeper into these concepts. I am covering digital notes of these books seeding soon in the garden here. The compiler used by V8 is called Turbofan, and its job is to optimize the code so that it runs faster, especially if the same section of code is executed multiple times.
Also, remember that Garbage Collection happens in parallel to free up memory. Below, I’ve listed the names of some garbage collectors used in the V8 engine—make sure to look them up and read about them.
How Byte code looks like ?
https://medium.com/dailyjs/understanding-v8s-bytecode-317d46c94775
Search These Keywords
- Turbofan
- Ignition Interpreter
- Bytecode
- Optimized Code
- JIT (Just-in-Time Compilation)
- Deoptimization in Compilation
- Inline Caching
- Copy Elision
- Orinoco Garbage Collector
- Oilpan Garbage Collector
- MCompact Garbage Collector
- Scavenger Garbage Collector
- Mark-and-Sweep Algorithm
- V8 Crankshaft Compiler
Questions
- How does the compiler optimize the code, and in what situations does it fail to optimize?
- Research other algorithms similar to the Mark-and-Sweep algorithm. What do they do, and how do they differ?
- Find out byte code in v8 engine github repo
Useful Links
- AST Explorer
- V8 Turbofan Documentation
- https://github.com/thlorenz/v8-perf/blob/master/crankshaft/compiler.md
- https://v8.dev/blog/launching-ignition-and-turbofan
Tips
- JavaScript is both an interpreted and compiled language, benefiting from various optimizations.
- Read blogs of v8 website
And thats all for this session, I'm Ashutosh Anand Tiwari, and I'm writing digital notes on Node.js. If you enjoy these notes, please share them with your friends. If you find any errors or have improvements, feel free to contribute by clicking edit icon on top bar of this page. If you're interested in writing the next episode's notes, visit this link. Let's learn together! Also, please consider giving a star to this repo. For any queries, let's connect here. Thank you…