𝐇𝐨𝐰 𝐭𝐡𝐞 𝐍𝐨𝐝𝐞.𝐣𝐬 𝐄𝐯𝐞𝐧𝐭 𝐋𝐨𝐨𝐩 𝐖𝐨𝐫𝐤𝐬 𝐈𝐧𝐭𝐞𝐫𝐧𝐚𝐥𝐥𝐲 At a high level, the Event Loop is a continuous cycle that checks for tasks, executes callbacks, and waits for new events. But internally, it’s more structured than most people think. It runs in phases, each handling specific types of callbacks: • 𝐓𝐢𝐦𝐞𝐫𝐬 → Executes callbacks from 𝘴𝘦𝘵𝘛𝘪𝘮𝘦𝘰𝘶𝘵() and 𝘴𝘦𝘵𝘐𝘯𝘵𝘦𝘳𝘷𝘢𝘭() whose delay has expired. • 𝐏𝐞𝐧𝐝𝐢𝐧𝐠 𝐂𝐚𝐥𝐥𝐛𝐚𝐜𝐤𝐬 → Runs certain system-level I/O callbacks deferred from the previous loop. • 𝐏𝐨𝐥𝐥 → Retrieves completed I/O events and executes their callbacks; this is where most request handling happens. • 𝐂𝐡𝐞𝐜𝐤 → Executes callbacks scheduled with 𝘴𝘦𝘵𝘐𝘮𝘮𝘦𝘥𝘪𝘢𝘵𝘦(). • 𝐂𝐥𝐨𝐬𝐞 𝐂𝐚𝐥𝐥𝐛𝐚𝐜𝐤𝐬 → Runs cleanup callbacks like socket.on('close'). • 𝐌𝐢𝐜𝐫𝐨𝐭𝐚𝐬𝐤 𝐐𝐮𝐞𝐮𝐞 (𝐛𝐞𝐭𝐰𝐞𝐞𝐧 𝐞𝐯𝐞𝐫𝐲 𝐩𝐡𝐚𝐬𝐞) → Executes 𝘱𝘳𝘰𝘤𝘦𝘴𝘴.𝘯𝘦𝘹𝘵𝘛𝘪𝘤𝘬() and resolved Promises before moving to the next phase. 𝐖𝐡𝐚𝐭 𝐓𝐡𝐢𝐬 𝐌𝐞𝐚𝐧𝐬 𝐏𝐫𝐚𝐜𝐭𝐢𝐜𝐚𝐥𝐥𝐲 • If you run heavy CPU code → the loop can’t move to the next phase • If the poll phase is blocked → no new requests are processed • If you flood microtasks → timers and I/O get delayed • Async I/O doesn’t block — but CPU work does If your Node.js app feels “slow,” measure event loop delay before blaming the database or framework. #NodeJS #JavaScript #BackendDevelopment #V8 #SystemDesign #PerformanceEngineering #EventLoop
Node.js Event Loop Phases: Understanding the Inner Workings
More Relevant Posts
-
A performance-focused package manager for Node.js and Bun. I’ve spent the last few weeks building JPM (Joint Package Manager), a lightweight package manager written in pure JavaScript using only Node.js core modules. The goal was to build a tool that feels faster and more secure for modern development. Here is how it handles the installation process: Parallel Execution: Uses a custom worker-pool to handle fetching and extraction concurrently. Platform-Aware: It filters optionalDependencies during resolution, so it only downloads the binaries compatible with your specific OS and CPU architecture. Security Defaults: Includes built-in Zip Slip protection, mandatory sha512 integrity checks, and a scanner for malicious patterns in package scripts. Monorepos (Hive): Handles workspaces with an intelligent hoisting system to keep node_modules efficient. JPM is fully compatible with the standard package.json ecosystem and works seamlessly across both Node.js and Bun runtimes. ------------------------------------------------------------------------------ npm i -g jpm-pkg ------------------------------------------------------------------------------ If you’re interested in the technical implementation or want to try it out, the source code and docs are available here: GitHub: https://lnkd.in/duqwcrb9 NPM: https://lnkd.in/dqzwiJPy #nodejs #javascript #webdevelopment #opensource #package-manager #softwareengineering
To view or add a comment, sign in
-
-
Node.js is often called single threaded… but it still handles multiple tasks at the same time. How? 🤔 This is where a lot of confusion starts. Yes, Node.js runs on a single main thread, but it’s not limited. The real strength comes from how it manages work behind the scenes ⚡ Here’s what’s really happening: • Event loop keeps the app responsive with non blocking I/O 🔄 • libuv thread pool handles background operations 🧩 • Worker threads take care of CPU heavy tasks 🧠 The idea is simple. • Main thread handles requests, callbacks, async flows • Heavy work gets offloaded to worker threads • Event loop stays free and fast 🚀 Because of this, you can: • Process large datasets • Run complex calculations • Handle parallel tasks All without slowing down your application. In real systems, this becomes critical. I’ve seen APIs freeze because of a single heavy operation. Moving that to worker threads instantly improved performance 📈 So Node.js isn’t multi threaded by default, but it’s built to scale intelligently when you use the right tools. Curious to hear, are you using worker threads in production or mostly relying on async patterns? 💬 #Nodejs #JavaScript #Backend #SystemDesign #Concurrency #WebDevelopment
To view or add a comment, sign in
-
-
🧵 Worker Threads vs Clusters in Node.js — do you know when to use which? Most developers know Node.js is single-threaded. But when your app needs real parallelism, you have two powerful tools at your disposal. Here's the breakdown: ⚙️ Worker Threads → Same process, shared memory → Perfect for CPU-heavy tasks: image processing, ML inference, file parsing → Communicate via postMessage() or SharedArrayBuffer → Lightweight — low overhead to spin up 🖥️ Clusters → Multiple processes, isolated memory → Perfect for scaling HTTP servers across all CPU cores → Communicate via IPC (inter-process communication) → Built on child_process.fork() under the hood → A single worker crash won't take down the whole app 🔑 The key mental model: • CPU-bound work? → Worker Threads • Network/I/O scaling? → Clusters And here's the pro move: combine both. Use Clusters to scale across cores, and Worker Threads within each process for heavy computation. Drop your Node.js parallelism questions below 👇 #NodeJS #JavaScript #BackendDevelopment #SoftwareEngineering #WebDevelopment
To view or add a comment, sign in
-
-
🚀 Deep-dived into Node.js architecture today — here's what every backend developer should understand: Node.js isn't just JavaScript on a server. It's a powerful orchestration of: ⚙️ V8 Engine — executes JS on the main thread 📡 libuv — handles async I/O and the thread pool 🔗 C++ Bindings — bridge your JS code to the OS 🔄 Event Loop — schedules callbacks without blocking The full execution flow: 𝗝𝗦 𝗰𝗼𝗱𝗲 → 𝗡𝗼𝗱𝗲 𝗔𝗣𝗜 → 𝗖++ 𝗯𝗶𝗻𝗱𝗶𝗻𝗴 → 𝗹𝗶𝗯𝘂𝘃 → 𝗢𝗦 → 𝗰𝗮𝗹𝗹𝗯𝗮𝗰𝗸 𝗾𝘂𝗲𝘂𝗲 → 𝗲𝘃𝗲𝗻𝘁 𝗹𝗼𝗼𝗽 → 𝗩𝟴 For CPU-intensive tasks? Worker threads spin up isolated V8 instances — real parallelism, zero main thread blocking. Understanding this architecture is the difference between writing Node.js code and writing high-performance server applications. #NodeJS #JavaScript #BackendDevelopment #SoftwareEngineering #WebDevelopment
To view or add a comment, sign in
-
When starting with Node.js, there’s a concept that confused me at first.. maybe not for many… but definitely for few of us 😄 We often hear that Node.js is asynchronous and non-blocking. But then we write code like: const data = await fetchUser(); And the first instinct is: "Wait… aren’t we literally waiting here?" At first it feels like a paradox. The clarity comes when you start thinking of Node.js like a CPU scheduler. The Event Loop acts like the scheduler, and our callbacks are like processes waiting to run. But just like any scheduler, not every task runs with the same priority For example: 1. process.nextTick() callbacks run first 2. Promise continuations ('then', 'await') run next 3. Timers and I/O callbacks come later This prioritization is intentional. It ensures small continuation tasks complete immediately instead of getting delayed behind timers or I/O work. Sometimes understanding Node.js isn’t just about async code. It’s about realizing that the Event Loop is really a carefully designed scheduling system. And like any good scheduler, the order of execution is not accidental, it's the architecture #NodeJS #EventLoop #AsyncProgramming #JavaScript
To view or add a comment, sign in
-
Understanding HTTP Status Codes 🌐 When starting your journey in web development, one fundamental concept you’ll constantly encounter is HTTP Status Codes. They are responses sent by the server that indicate whether a request worked correctly or if something went wrong. Here are some common status codes every developer should recognize: ✅ 200 OK → The request was completed successfully 🔁 301 Moved Permanently → The resource has been redirected to another URL ⚠️ 400 Bad Request → The server could not process the request 🔒 401 Unauthorized → Authentication is required to access the resource ❌ 404 Not Found → The requested page or resource was not found 🔥 500 Internal Server Error → An unexpected error occurred on the server 💡 Developer Tip: While debugging, check your browser’s DevTools → Network tab to monitor request responses and status codes. Understanding these responses helps you see how frontend and backend communicate in real time. #WebDevelopment #Coding #Frontend #React #JavaScript #Python
To view or add a comment, sign in
-
-
Not every Angular feature needs global state. But many apps start there. Global Store • Single source of truth • Shared across the application • Ideal for cross-feature data Examples: user session global settings permissions But sometimes global state becomes too global. That’s where Component Store shines. Component Store • Feature-scoped state • Encapsulated logic • Less boilerplate than NgRx Store Think of it like this: Global Store → App-level state Component Store → Feature-level state Good architecture isn't about choosing one. It's about choosing the right scope. 👇 Where do you keep most of your state today? #Angular #StateManagement #NgRx #ComponentStore #FrontendArchitecture #JavaScript #CleanCode #DeveloperTips
To view or add a comment, sign in
-
-
🚨 Node.js is NOT actually single-threaded One of the biggest misconceptions I had early in backend development: “Node.js can only handle one thing at a time.” Not true. Node runs JavaScript on a single thread but under the hood it uses: Event Loop Worker Threads libuv thread pool Meaning: - File system operations - Database queries - Network requests are executed outside the main thread. The real bottleneck isn’t Node itself, it’s CPU-blocking code. 💡 Lesson: Node excels at I/O-heavy systems, not CPU-heavy computation. That’s why companies use Node for APIs but offload heavy processing to workers or services. 💬 Question: What surprised you most when you learned how Node actually works? #NodeJS #BackendEngineering #SystemDesign #JavaScript #FullStackDev #SoftwareEngineering #TechInsights
To view or add a comment, sign in
-
-
I recently ran into a debugging issue that many frontend engineers may relate to. My pagination worked perfectly in development, but once we deployed to pilot, the **Next button stopped fetching new data**. The URL updated. The page number changed. But the API never fired again. After digging deeper, I discovered the real issue wasn’t pagination at all — it was **React Query caching caused by a static query key**. Because the query key didn’t include the dynamic parameters (pageNumber), React Query kept returning cached data instead of refetching. I wrote a short article explaining: • what caused the issue • why it only appeared outside development • and the small change that fixed it If you're working with React Query, pagination, or server-state caching, this lesson might save you hours of debugging. Read the story here: https://lnkd.in/ej6t5Tnf Curious if others have encountered similar caching issues when moving from dev to staging or production. #React #ReactQuery #JavaScript #WebDevelopment #SoftwareEngineering
To view or add a comment, sign in
-
More from this author
Explore content categories
- Career
- Productivity
- Finance
- Soft Skills & Emotional Intelligence
- Project Management
- Education
- Technology
- Leadership
- Ecommerce
- User Experience
- Recruitment & HR
- Customer Experience
- Real Estate
- Marketing
- Sales
- Retail & Merchandising
- Science
- Supply Chain Management
- Future Of Work
- Consulting
- Writing
- Economics
- Artificial Intelligence
- Employee Experience
- Workplace Trends
- Fundraising
- Networking
- Corporate Social Responsibility
- Negotiation
- Communication
- Engineering
- Hospitality & Tourism
- Business Strategy
- Change Management
- Organizational Culture
- Design
- Innovation
- Event Planning
- Training & Development