VibeKoding / Ensiklopedia Β· Fondasi KuatEnsiklopedia Β· Fondasi Kuat / Principles of JavaScript RuntimePrinciples of JavaScript Runtime
VK

Principles of JavaScript RuntimePrinciples of JavaScript Runtime

πŸ“š Ensiklopedia Β· Fondasi KuatEnsiklopedia Β· Fondasi Kuat 🌏 Dual Bahasa (ID / EN) ⚑ VibeKoding Native

Ensiklopedia VibeKoding: Principles of JavaScript Runtime.Ensiklopedia VibeKoding: Principles of JavaScript Runtime.

πŸ’‘ Tips PraktisπŸ’‘ Pro Tip

You've already learned JavaScript basics, but have you ever wondered: - Where exactly does your code run? - Why does the same code behave differently in the browser versus Node.js? - Why does code sometimes "freeze up," while at other times it seems to run "in parallel"? This article will take you deep into the JavaScript runtime environment, including the event loop, call stack, memory management, and more. After reading this, you'll understand why code executes in a particular order, quickly locate async-related bugs, optimize code performance, and avoid memory leaks.You've already learned JavaScript basics, but have you ever wondered: - Where exactly does your code run? - Why does the same code behave differently in the browser versus Node.js? - Why does code sometimes "freeze up," while at other times it seems to run "in parallel"? This article will take you deep into the JavaScript runtime environment, including the event loop, call stack, memory management, and more. After reading this, you'll understand why code executes in a particular order, quickly locate async-related bugs, optimize code performance, and avoid memory leaks.

What will you learn in this article?What will you learn in this article?

ChapterContentWhat you'll be able to do
Chapter 1Runtime overviewUnderstand where JavaScript code runs
Chapter 2Browser runtimeKnow what Web APIs the browser provides
Chapter 3Node.js runtimeUnderstand the server-side JavaScript environment
Chapter 4Event loop deep diveMaster the execution order of macrotasks and microtasks
Chapter 5Call stack and memoryUnderstand code execution and memory management
Chapter 6Practical tipsOptimize performance and debug memory leaks

------

1. Runtime Overview1. Runtime Overview

πŸ’‘ Tips PraktisπŸ’‘ Pro Tip

What is a "runtime"? JavaScript is just a language β€” why does the same code behave differently in different environments?What is a "runtime"? JavaScript is just a language β€” why does the same code behave differently in different environments?

1.1 Overview of a Runtime1.1 Overview of a Runtime

Runtime = JavaScript Engine + Environment-provided APIsRuntime = JavaScript Engine + Environment-provided APIs

If JavaScript is the "programming language," then the runtime is the "operating system" β€” it determines what your code can and cannot do.If JavaScript is the "programming language," then the runtime is the "operating system" β€” it determines what your code can and cannot do.

CODE
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ JavaScript Code β”‚ β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€ β”‚ JavaScript Engine (V8) β”‚ ← Responsible for parsing and executing code β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€ β”‚ Runtime Environment (Browser/Node.js) β”‚ ← Provides additional capabilities β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

An analogy: JavaScript is "Mandarin," the runtime is the "city"An analogy: JavaScript is "Mandarin," the runtime is the "city"

1.2 Two Mainstream Runtimes1.2 Two Mainstream Runtimes

FeatureBrowserNode.js
Primary useWeb interaction, user interfacesServer-side applications, CLI tools
Global objectwindowglobal
DOM APIβœ… Supported❌ Not supported
File system❌ Limitedβœ… Full support
Module systemES ModulesCommonJS + ES Modules
TimerssetTimeout, setIntervalsetTimeout, setInterval
Network requestsfetch, XMLHttpRequesthttp, https modules

πŸ‘‡ Try it out: Compare the environment differences between the browser and Node.jsπŸ‘‡ Try it out: Compare the environment differences between the browser and Node.js

πŸ“– Konsep PentingπŸ“– Core Concept

The runtime determines which APIs you can use. DOM APIs available in the browser won't work in Node.js; file APIs available in Node.js won't work in the browser. That's why some code needs "environment detection."The runtime determines which APIs you can use. DOM APIs available in the browser won't work in Node.js; file APIs available in Node.js won't work in the browser. That's why some code needs "environment detection."

------

2. Browser Runtime2. Browser Runtime

πŸ’‘ Tips PraktisπŸ’‘ Pro Tip

What capabilities does the browser provide for JavaScript to manipulate web pages?What capabilities does the browser provide for JavaScript to manipulate web pages?

2.1 Components of the Browser Runtime2.1 Components of the Browser Runtime

CODE
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ JavaScript Engine β”‚ β”‚ (V8 / SpiderMonkey) β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ ↓ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ Web APIs β”‚ β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ β”‚ β”‚ DOM β”‚ β”‚ BOM β”‚ β”‚ Network β”‚ β”‚ β”‚ β”‚Manipulateβ”‚ β”‚Manipulateβ”‚ β”‚ Network β”‚ β”‚ β”‚ β”‚ pages β”‚ β”‚ browser β”‚ β”‚ requests β”‚ β”‚ β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ ↓ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ Event Loop β”‚ β”‚ Coordinates code execution, event β”‚ β”‚ handling, and task scheduling β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

2.2 Three Categories of Web APIs2.2 Three Categories of Web APIs

1. DOM API - Manipulate page content1. DOM API - Manipulate page content

javascript
// Find elements const title = document.querySelector('h1') // Modify content title.textContent = 'New Title' // Add styles title.style.color = 'red'

2. BOM API - Manipulate the browser2. BOM API - Manipulate the browser

javascript
// Page navigation window.location.href = 'https://example.com' // Browser storage localStorage.setItem('key', 'value') // Browser history history.back()

3. Network API - Network requests3. Network API - Network requests

javascript
// Send HTTP request fetch('/api/data') .then(response => response.json()) .then(data => console.log(data))

2.3 Browser-Specific Event Mechanism2.3 Browser-Specific Event Mechanism

One of the most powerful features of the browser runtime is "event-driven" programming β€” code doesn't need to run continuously, but executes when the user performs actions.One of the most powerful features of the browser runtime is "event-driven" programming β€” code doesn't need to run continuously, but executes when the user performs actions.

javascript
button.addEventListener('click', () => { console.log('Button was clicked') })

Common event types:Common event types:

Event typeWhen triggeredPractical scenario
clickMouse clickButton interaction
inputInput field content changesReal-time search
scrollPage scrollingLazy loading
loadResource finished loadingInitialize data
errorError occurredError handling

------

3. Node.js Runtime3. Node.js Runtime

πŸ’‘ Tips PraktisπŸ’‘ Pro Tip

What enables JavaScript to run on the server side?What enables JavaScript to run on the server side?

3.1 Components of Node.js3.1 Components of Node.js

CODE
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ JavaScript Engine β”‚ β”‚ (V8) β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ ↓ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ Node.js Built-in Modules β”‚ β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ β”‚ β”‚ fs β”‚ β”‚ http β”‚ β”‚ path β”‚ β”‚ β”‚ β”‚ File β”‚ β”‚ HTTP β”‚ β”‚ Path β”‚ β”‚ β”‚ β”‚operationsβ”‚ β”‚ server β”‚ β”‚ handling β”‚ β”‚ β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ ↓ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ libuv Event Loop Library β”‚ β”‚ Cross-platform async I/O support β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

3.2 Node.js-Specific Capabilities3.2 Node.js-Specific Capabilities

1. File System Operations1. File System Operations

javascript
const fs = require('fs') // Read file fs.readFile('./data.txt', 'utf8', (err, data) => { if (err) throw err console.log(data) }) // Write file fs.writeFile('./output.txt', 'Hello', (err) => { if (err) throw err console.log('Write successful') })

2. HTTP Server2. HTTP Server

javascript
const http = require('http') const server = http.createServer((req, res) => { res.writeHead(200, { 'Content-Type': 'text/html' }) res.end('<h1>Hello World</h1>') }) server.listen(3000)

3. Module System3. Module System

javascript
// CommonJS (Node.js default) const fs = require('fs') module.exports = { myFunction } // ES Modules (modern approach) import fs from 'fs' export { myFunction }

3.3 Browser vs Node.js Comparison3.3 Browser vs Node.js Comparison

FeatureBrowserNode.js
Entry fileHTML fileJavaScript file
Global objectswindow, documentglobal, process
Module loading