Node.js Execution Model: JavaScript, V8 Engine, and libuv

What happens inside Node.js when our JavaScript runs? While learning Node.js, I tried to understand what actually happens under the hood when JavaScript executes. Here is the simple mental model I built today. 1. JavaScript is synchronous by default JavaScript runs one line at a time. It uses a single call stack, which means only one operation runs at a time. 2. Code runs inside the V8 engine JavaScript runs inside the V8 engine (the same engine used in Chrome). Inside the engine there are two main parts: • Memory Heap – where variables and objects are stored • Call Stack – where functions are executed Whenever code runs, an Execution Context is created and pushed into the call stack. After the code finishes, that context is removed from the stack. 3. Garbage Collector V8 also has a garbage collector. It removes unused variables from memory so the application does not keep unnecessary data. 4. JavaScript itself cannot handle timers or system tasks JavaScript alone does not know how to deal with things like: • timers • file system operations • network requests • operating system tasks These capabilities come from the environment where JavaScript runs. 5. Node.js provides these capabilities Node.js provides APIs that allow JavaScript to interact with the system, such as files, network and timers. Internally, Node.js uses a C library called libuv. 6. Role of libuv libuv sits between the JavaScript engine and the operating system. It helps Node.js handle asynchronous tasks such as: • file operations • network requests • timers • background I/O tasks This is how Node.js enables non-blocking and asynchronous behaviour, even though JavaScript itself is single-threaded. Simple mental model JavaScript Code ↓ V8 Engine (Call Stack + Memory Heap) ↓ Node.js APIs ↓ libuv ↓ Operating System #NodeJS #JavaScript #BackendDevelopment #WebDevelopment #LearningInPublic

  • No alternative text description for this image

To view or add a comment, sign in

Explore content categories