Node.js
What is V8?
Learn what the V8 engine is and how it works.
What is V8?
V8 is Google's open-source JavaScript engine. Node.js, Chrome and Electron are built on V8. V8 compiles and executes JavaScript as machine code, which lets code run fast outside the browser.
V8 uses a JIT (just-in-time) compiler: it compiles code at runtime instead of interpreting it. This yields much faster execution than interpreted languages. V8 also handles memory management (garbage collection) and object internals (hidden classes) itself. As a Node.js developer, knowing what V8 does explains why certain patterns are fast. This lesson surveys the core building blocks of V8.
JIT compilation
V8 compiles JavaScript at runtime with JIT instead of interpreting it. Code is first turned into bytecode quickly, then frequently run sections are promoted to machine code. This balances slow interpretation and slow full compilation.
When a function is first called V8 compiles it quickly. Once the call count crosses a threshold V8 marks the function hot and applies more aggressive optimization. Example: function isHot(callCount) { return callCount >= 100; }. A function called 100 times is considered hot. Thanks to JIT, Node.js is not as fast as C but much faster than interpreted languages.
Exercise Describe the V8 engine
Write a v8Engine function. Return {name: 'V8', language: 'JavaScript', type: 'JIT'}. Print with console.log(v8Engine().name + ': ' + v8Engine().type).
Show example solution
function v8Engine() {
return {name: 'V8', language: 'JavaScript', type: 'JIT'};
}
console.log(v8Engine().name + ': ' + v8Engine().type);
Test yourself
-
Which engine is Node.js built on?
- SpiderMonkey
- Chakra
- V8
- JavaScriptCore
Show answer
V8 — Node.js is built on the V8 engine.
-
Return the V8 engine type
function v8Engine() { return {name: 'V8', language: 'JavaScript', type: '____'}; }
Show answer
function v8Engine() { return {name: 'V8', language: 'JavaScript', type: 'JIT'}; }
-
Match each concept to its description
- V8
- JavaScript engine
- JIT
- Compile at runtime
- Node.js
- Built on V8
- Chrome
- Browser using V8
Next lesson: JIT compilation