Runtime Engines: How JavaScript Executes

Runtime Engines: How JavaScript Executes

A deep dive into how JavaScript engines like V8, JavaScriptCore, and Hermes parse, interpret, JIT-compile, and garbage-collect code, and how runtimes such as Node.js, Deno, and Bun build on top of them.

JavaScriptV8Node.jsBunPerformance

Engine vs Runtime: Two Different Layers

Developer တွေ နေ့စဉ်ပြောနေကြတဲ့ "Node.js က မြန်တယ်"၊ "Bun က Node ထက် မြန်တယ်" ဆိုတဲ့ စကားတွေထဲမှာ Engine နဲ့ Runtime ဆိုတဲ့ Layer နှစ်ခုကို ရောထွေးပြီး ပြောနေကြတာ တွေ့ရတတ်ပါတယ်။ တကယ်တော့ ဒီနှစ်ခုဟာ တာဝန်ချင်း လုံးဝမတူပါဘူး။

  • JavaScript Engine (V8, JavaScriptCore, SpiderMonkey, Hermes): ECMAScript Specification အတိုင်း Code ကို Parse လုပ်ခြင်း၊ Execute လုပ်ခြင်း၊ Memory စီမံခြင်းတို့ကိုသာ တာဝန်ယူပါတယ်။ Engine သက်သက်မှာ setTimeout, fetch, fs လိုမျိုး API တွေ မပါဝင်ပါဘူး။
  • Runtime (Node.js, Deno, Bun, Browser): Engine ကို Embed လုပ်ထားပြီး Event Loop၊ File System၊ Network၊ Timers စတဲ့ Host APIs တွေကို ထပ်ဖြည့်ပေးတဲ့ Environment ဖြစ်ပါတယ်။

ဥပမာပြောရရင် Node.js ဆိုတာ V8 Engine ကို libuv (Async I/O Library) နဲ့ C++ Bindings တွေနဲ့ ပေါင်းစပ်ထားတာ ဖြစ်ပါတယ်။ ဒါကြောင့် Performance ပြဿနာ တစ်ခုကို စစ်ဆေးတဲ့အခါ Engine အဆင့် (CPU-bound Code Execution) ပြဿနာလား၊ Runtime အဆင့် (I/O Scheduling) ပြဿနာလား ဆိုတာကို အရင်ခွဲခြားနိုင်ဖို့ လိုအပ်ပါတယ်။

From Source Code to Bytecode

Engine တစ်ခုက JavaScript File ကို လက်ခံရရှိတဲ့အခါ ချက်ချင်း Machine Code အဖြစ် ပြောင်းလဲတာ မဟုတ်ပါဘူး။ ပထမဆုံး Parser က Source Code ကို Tokens အဖြစ် ခွဲထုတ်ပြီး Abstract Syntax Tree (AST) တည်ဆောက်ပါတယ်။

ဒီနေရာမှာ စိတ်ဝင်စားစရာ Optimization တစ်ခုက Lazy Parsing ပါ။ Page Load အချိန်မှာ ချက်ချင်း မခေါ်သေးတဲ့ Functions တွေကို V8 က Pre-parser နဲ့ Syntax Error ရှိမရှိလောက်ပဲ အမြန်စစ်ပြီး Full AST မဆောက်ဘဲ ချန်ထားပါတယ်။ အဲဒီ Function ကို တကယ်ခေါ်တော့မှသာ အပြည့်အစုံ Parse လုပ်ပါတယ်။ Bundle Size ကြီးတဲ့ Application တွေမှာ Startup Time ကို သိသိသာသာ သက်သာစေတာ ဒီနည်းလမ်းကြောင့် ဖြစ်ပါတယ်။

AST ရပြီဆိုရင် V8 ရဲ့ Ignition Interpreter က AST ကို Compact ဖြစ်တဲ့ Bytecode အဖြစ် ပြောင်းလဲပြီး စတင် Execute လုပ်ပါတယ်။ Bytecode ဟာ Machine Code ထက် Memory နည်းနည်းပဲ ယူတဲ့အတွက် Mobile Devices တွေမှာ အထူးအရေးပါပါတယ်။

Tiered JIT Compilation in V8

Interpreter တစ်ခုတည်းနဲ့ဆိုရင် Code က Run လို့ရပေမဲ့ မြန်ဆန်မှု မရှိပါဘူး။ ဒါကြောင့် ခေတ်မီ Engines တွေဟာ Tiered Just-In-Time (JIT) Compilation ကို အသုံးပြုကြပါတယ်။ Code တစ်ခုကို ခေါ်တဲ့အကြိမ်ရေ များလာလေ (Hot Code) ပိုမြင့်တဲ့ Tier ကို တဖြည်းဖြည်း မြှင့်တင်ပေးလေ ဖြစ်ပါတယ်။

Loading flowchart...
  • Sparkplug: Bytecode ကို Optimization မလုပ်ဘဲ Machine Code အဖြစ် အမြန်ပြောင်းပေးပါတယ်။ Compile Time အလွန်နည်းပါတယ်။
  • Maglev: Type Feedback ကို အခြေခံပြီး အလယ်အလတ်အဆင့် Optimization လုပ်ပေးပါတယ်။ Compile မြန်ပြီး ရလဒ်ကောင်းကောင်းရဖို့ ချိန်ညှိထားတဲ့ Tier ဖြစ်ပါတယ်။
  • TurboFan: အကြိမ်ပေါင်း ထောင်ချီ ခေါ်ခံရတဲ့ Hot Functions တွေအတွက် Inlining၊ Escape Analysis စတဲ့ နက်ရှိုင်းတဲ့ Optimization တွေ လုပ်ပြီး အမြန်ဆုံး Machine Code ထုတ်ပေးပါတယ်။

ဒီ Tier ခွဲခြားမှုရဲ့ အဓိက Trade-off က Compile Time နဲ့ Execution Speed ကြားက ချိန်ခွင်လျှာ ဖြစ်ပါတယ်။ တစ်ကြိမ်ပဲ Run မယ့် Code ကို TurboFan နဲ့ Optimize လုပ်ရင် Compile ချိန်က Execute ချိန်ထက် ပိုကုန်သွားနိုင်ပါတယ်။

Hidden Classes and Inline Caches

JavaScript ဟာ Dynamic Language ဖြစ်တဲ့အတွက် Object တစ်ခုရဲ့ Property ကို Runtime မှာ အချိန်မရွေး ထပ်ထည့်၊ ဖျက်လို့ရပါတယ်။ ဒါကို မြန်မြန်ဆန်ဆန် Access လုပ်နိုင်ဖို့ Engine တွေက Hidden Classes (V8 မှာ Maps၊ SpiderMonkey မှာ Shapes လို့ခေါ်ပါတယ်) ကို နောက်ကွယ်မှာ တည်ဆောက်ပါတယ်။ Property Layout တူတဲ့ Objects တွေဟာ Hidden Class တစ်ခုတည်းကို မျှဝေသုံးစွဲကြပါတယ်။

function createPoint(x, y) {
  return { x, y }
}

const a = createPoint(1, 2) // shape: {x, y}
const b = createPoint(3, 4) // same shape, shared hidden class

const c = { y: 2, x: 1 }    // different property order -> new shape
const d = { x: 1 }
d.y = 2                     // late addition -> extra shape transition
delete a.x                  // may fall back to slow dictionary mode

Hidden Class နဲ့တွဲပြီး အလုပ်လုပ်တာက Inline Cache (IC) ဖြစ်ပါတယ်။ point.x ကို ပထမအကြိမ် Access လုပ်တဲ့အခါ Engine က Hidden Class နဲ့ Memory Offset ကို မှတ်ထားလိုက်ပြီး နောက်တစ်ကြိမ်မှာ Lookup မလုပ်တော့ဘဲ တိုက်ရိုက်ဖတ်ပါတယ်။ Function တစ်ခုက Shape တစ်မျိုးတည်းကိုပဲ လက်ခံရင် Monomorphic ဖြစ်ပြီး အမြန်ဆုံးပါ။ Shape ၂ မျိုးကနေ ၄ မျိုးထိဆိုရင် Polymorphic၊ အဲဒီထက်များရင်တော့ Megamorphic ဖြစ်သွားပြီး Cache အကျိုးကျေးဇူး ဆုံးရှုံးသွားပါတယ်။

လက်တွေ့မှာတော့ Objects တွေကို Constructor သို့မဟုတ် Factory Function တစ်ခုတည်းကနေ Property အစီအစဉ်တူတူ ဖန်တီးတာ၊ နောက်မှ Property ထပ်မထည့်တာ၊ delete ကို ရှောင်တာတွေက Engine ကို ကူညီပေးရာ ရောက်ပါတယ်။

Deoptimization: When Assumptions Break

JIT Compiler တွေဟာ Type Feedback ပေါ် အခြေခံပြီး ခန့်မှန်းချက် (Speculation) တွေနဲ့ Optimize လုပ်ကြပါတယ်။ ဥပမာ add Function ကို Integer တွေနဲ့ပဲ အကြိမ်ပေါင်း သန်းချီ ခေါ်ထားရင် TurboFan က "ဒီ Function ကို Integer ပဲ ဝင်လာမယ်" လို့ ယူဆပြီး Integer Addition Machine Code ထုတ်ပေးပါတယ်။

function add(a, b) {
  return a + b
}

for (let i = 0; i < 1e6; i++) add(i, 1) // optimized for small integers
add('1', 2)                             // assumption breaks -> deoptimization

String တစ်ခု ရုတ်တရက် ဝင်လာတဲ့အခါ Optimized Code ထဲက Guard စစ်ဆေးချက်က မကိုက်ညီတော့တဲ့အတွက် Engine က အဲဒီ Machine Code ကို စွန့်ပစ်ပြီး Ignition Bytecode ဆီ ပြန်ဆင်းသွားပါတယ်။ ဒါကို Deoptimization လို့ခေါ်ပါတယ်။ ခဏခဏ Deopt ဖြစ်နေတဲ့ Hot Path တစ်ခုဟာ Optimize → Deopt → Re-optimize သံသရာထဲ ပိတ်မိပြီး CPU ကို အလဟဿ သုံးနေနိုင်ပါတယ်။ Node.js မှာ ဒါကို အောက်ပါ Flags တွေနဲ့ စစ်ဆေးကြည့်နိုင်ပါတယ်။

node --trace-opt --trace-deopt server.js

Garbage Collection Inside the Engine

Memory စီမံခန့်ခွဲမှုဟာလည်း Engine ရဲ့ တာဝန် ဖြစ်ပါတယ်။ V8 ဟာ "Objects အများစုဟာ မွေးပြီးမကြာခင် သေတယ်" ဆိုတဲ့ Generational Hypothesis ကို အခြေခံပြီး Heap ကို နှစ်ပိုင်း ခွဲထားပါတယ်။

  • Young Generation: Object အသစ်တွေ နေရာယူတဲ့ နေရာဖြစ်ပြီး Scavenger (Semi-space Copying Collector) က မကြာခဏ အမြန် ရှင်းလင်းပေးပါတယ်။
  • Old Generation: GC အကြိမ်ကြိမ် ကျော်လွှားလာတဲ့ Objects တွေကို ရွှေ့ထားပြီး Mark-Compact နဲ့ ရှင်းပါတယ်။ V8 ရဲ့ Orinoco Project ကြောင့် Marking အလုပ်အများစုကို Background Threads တွေမှာ Concurrent ပုံစံ လုပ်ဆောင်ပြီး Main Thread ရပ်တန့်ချိန် (Stop-the-World Pause) ကို လျှော့ချထားပါတယ်။

Backend Service တွေမှာ Request တစ်ခုချင်းစီအတွက် Large Objects တွေ ဆက်တိုက်ဖန်တီးနေရင် GC Pressure တက်လာပြီး P99 Latency မှာ Spike တွေ ပေါ်လာတတ်ပါတယ်။ Container ထဲမှာ Run တဲ့အခါ --max-old-space-size ကို Container Memory Limit နဲ့ ကိုက်ညီအောင် သတ်မှတ်ပေးဖို့လည်း သတိထားရပါမယ်။

Comparing Engines and Runtimes

Engine တစ်ခုချင်းစီဟာ မတူညီတဲ့ ဦးစားပေးမှုတွေနဲ့ ဒီဇိုင်းထုတ်ထားကြပါတယ်။

EngineMaintainerExecution TiersUsed By
V8GoogleIgnition, Sparkplug, Maglev, TurboFanChrome, Node.js, Deno, Cloudflare Workers
JavaScriptCoreAppleLLInt, Baseline JIT, DFG, FTLSafari, Bun
SpiderMonkeyMozillaBaseline Interpreter, Baseline JIT, WarpFirefox
HermesMetaAOT Bytecode, InterpreterReact Native

Hermes ကတော့ ထူးခြားပါတယ်။ Mobile App ဖွင့်ချိန် (TTI) မြန်ဖို့အတွက် JavaScript ကို Build Time မှာကတည်းက Bytecode အဖြစ် Ahead-of-Time (AOT) Compile လုပ်ထားပြီး Device ပေါ်မှာ Parse လုပ်ရတဲ့ ဝန်ကို ဖယ်ရှားထားပါတယ်။

RuntimeEngineImplementation LanguageI/O Layer
Node.jsV8C++libuv
DenoV8RustTokio
BunJavaScriptCoreZigCustom event loop

Choosing a Runtime in Practice

Benchmark တွေမှာ Bun က Startup Time နဲ့ HTTP Throughput မြင့်တာ တွေ့ရပေမဲ့၊ အဲဒီ Benchmark အများစုဟာ Database Query သို့မဟုတ် External API ခေါ်တာမျိုး မပါတဲ့ Hello World Scenario တွေ ဖြစ်ပါတယ်။ လက်တွေ့ Production Workload အများစုမှာ Bottleneck ဟာ Engine Speed ထက် Network I/O နဲ့ Database Latency မှာ ရှိနေတတ်ပါတယ်။

ဒါကြောင့် Runtime ရွေးချယ်တဲ့အခါ Raw Speed တစ်ခုတည်းကို မကြည့်ဘဲ npm Ecosystem Compatibility၊ Native Addons ပံ့ပိုးမှု၊ Observability Tooling နဲ့ Long-Term Support (LTS) ရှိမရှိကို ထည့်သွင်းစဉ်းစားသင့်ပါတယ်။ Enterprise Backend တွေအတွက် Node.js LTS ဟာ အန္တရာယ်အနည်းဆုံး ရွေးချယ်မှုဖြစ်ပြီး၊ Built-in TypeScript နဲ့ Permission Model လိုချင်ရင် Deno၊ CLI Tools နဲ့ Fast Test Runner လိုချင်ရင် Bun ကို ရွေးချယ်နိုင်ပါတယ်။

Final Thoughts

JavaScript Engine ဆိုတာ Code ကို တိုက်ရိုက် Run ပေးတဲ့ Black Box တစ်ခု မဟုတ်ဘဲ Parsing၊ Interpretation၊ Speculative JIT Compilation နဲ့ Garbage Collection တို့ ပေါင်းစပ်ထားတဲ့ ရှုပ်ထွေးတဲ့ စနစ်တစ်ခု ဖြစ်ပါတယ်။ Runtime ကတော့ အဲဒီ Engine ပေါ်မှာ I/O နဲ့ Host APIs တွေကို ထပ်ဆင့်ပေးတဲ့ Layer သာ ဖြစ်ပါတယ်။

Developer တွေအနေနဲ့ Engine Internals ကို အသေးစိတ် မှတ်မိနေဖို့ မလိုပေမဲ့ Object Shape တွေကို တည်ငြိမ်အောင်ထားခြင်း၊ Hot Path တွေမှာ Types ကို မရောထွေးခြင်းနဲ့ မလိုအပ်တဲ့ Allocation တွေ လျှော့ချခြင်းဆိုတဲ့ အခြေခံ စည်းမျဉ်းတွေကို လိုက်နာရုံနဲ့ Engine ရဲ့ Optimization တွေကို အပြည့်အဝ အကျိုးယူနိုင်ပါတယ်။ Optimize မလုပ်ခင် အမြဲတမ်း Profile အရင်လုပ်ဖို့တော့ မမေ့ပါနဲ့။