Stop using 𝙖𝙬𝙖𝙞𝙩 inside loops. You are killing your backend performance. 🐌 If the tasks don't depend on each other, don't run them sequentially. Use 𝙋𝙧𝙤𝙢𝙞𝙨𝙚.𝙖𝙡𝙡() to fire them concurrently. async function processUsers(userIds) { for (const id of userIds) { await db.getUser(id); // 𝗪𝗮𝗶𝘁𝘀 𝗳𝗼𝗿 𝗽𝗿𝗲𝘃𝗶𝗼𝘂𝘀 𝗼𝗻𝗲 𝘁𝗼 𝗳𝗶𝗻𝗶𝘀𝗵... } } async function processUsers(userIds) { await Promise.all(userIds.map(id => db.getUser(id))); // 𝗙𝗶𝗿𝗲𝘀 𝗮𝗹𝗹 𝗮𝘁 𝗼𝗻𝗰𝗲! } Sequential = 3s. Concurrent = 1s. It’s that simple. Note: Promise.all is great for small/medium I/O workloads; for heavy ones, batching or a limiter works best. I have recently started writing on X. Mind connecting?https://lnkd.in/dNbHZ6WF #nodejs #reactjs #javascript #webdevelopment #bhadreshpithwa #webdeveloperguide
Optimize Node.js Performance with Promise.all
More Relevant Posts
-
📣 JavaScript More Predictable with .toSorted() and .toReversed() One of my favorite additions to JavaScript is .toSorted() and .toReversed() — they return a new array instead of mutating the original one. 🔍 Why this matters: Accidental mutations with sort() and reverse() have caused bugs in so many codebases — especially when dealing with shared or stateful data. ✅ No side effects ✅ Cleaner, more functional patterns ✅ Available in modern runtimes (Node.js 20+, modern browsers) 🧠 If you're still using .sort() and doing slice() to clone first — it’s time to upgrade. Have you started using .toSorted() or .toReversed() yet? #NodeJS #JavaScript #WebDevelopment #Tech #DesignPatterns #FrontendDevelopment #WebDevelopment #DeveloperLife #nodejs #backend #backenddeveloper #TypeScript #CodingTips #DeveloperBestPractices
To view or add a comment, sign in
-
-
Are you able to answer these Promises scenarios ? Scenario 1: You trigger 5 parallel API calls using Promise.all. Two of them reject immediately. Three are still running. Question: Will the remaining 3 promises stop executing? Answer: No. Promise.all fails fast in terms of result, but it does NOT cancel the other promises. JavaScript has no built-in cancellation mechanism. The remaining promises continue running in the background. Interview follow-up: So how would you actually cancel them? Correct direction: AbortController (for fetch) or a custom cancellation pattern. Promise.all does not give you control over cancellation. Scenario 2: You use Promise.any with 4 APIs. All of them fail. Question: What does it throw? Answer: It throws an AggregateError, not a normal Error. And inside it, you get an errors array containing all rejection reasons. Many experienced developers miss this. Scenario 3: You use Promise.race for request timeout handling. Example logic: Race between API call and a timeout promise. Question: What happens if the API resolves after timeout already rejected? Answer: The API promise still resolves later. Race only determines which result you get first. It does not stop the slower promise. This creates hidden memory leaks if you don’t abort the request. Scenario 4: You want: Return first successful API. But if all fail, show a combined error report. Which method? Correct answer: Promise.any. But here’s the catch: If your environment doesn’t support it (older Node versions), you need a polyfill or custom implementation. Tip: Promise.all → Fail fast Promise.allSettled → Never fail Promise.race → First settled wins Promise.any → First success wins But none of them cancel anything. For more insightful content checkout below: 🟦 𝑳𝒊𝒏𝒌𝒆𝒅𝑰𝒏 - https://lnkd.in/dwi3tV83 ⬛ 𝑮𝒊𝒕𝑯𝒖𝒃 - https://lnkd.in/dkW958Tj 🟥 𝒀𝒐𝒖𝑻𝒖𝒃𝒆 - https://lnkd.in/dDig2j75 or Priya Frontend Vlogz 🔷 𝐓𝐰𝐢𝐭𝐭𝐞𝐫 - https://lnkd.in/dyfEuJNt #frontend #javascript #react #reactjs #html #css #typescript #es6 #interviewquestions #interview #interviewpreparation
To view or add a comment, sign in
-
-
JavaScript Module Formats — Why So Many? Ever opened a package and wondered why there are so many build files? index.ts index.js index.cjs index.mjs index.es.js index.umd.js It’s not duplication — it’s compatibility. Each file exists to support a different environment: 🔹 TypeScript for development 🔹 CommonJS for traditional Node.js 🔹 ES Modules for modern bundlers and tree-shaking 🔹 UMD for universal browser + Node usage When building libraries, understanding these formats helps you: - Avoid import/export errors - Support multiple environments - Improve performance with proper tree-shaking - Publish cleaner, production-ready packages Sometimes, growth as a developer isn’t about learning a new framework — it’s about understanding the fundamentals better. What module format do you default to in your projects? #JavaScript #TypeScript #NodeJS #Frontend #Backend #SoftwareEngineering #DevTips
To view or add a comment, sign in
-
-
🚀 Are you still using require or have you fully moved to import? As Node.js matures, the "Module War" between CommonJS (CJS) and ES Modules (ESM) is something every developer needs to master. If you’ve ever seen the error Cannot use import statement outside a module, this post is for you. 🚀 Here is the breakdown of the "Big Three" you'll see in your projects today: 1. CommonJS (.cjs or default .js) The "Classic" Node.js way. It’s synchronous and has been the backbone of the ecosystem for a decade. Keywords: require(), module.exports Best for: Legacy projects, quick scripts, and tools that haven't migrated yet. Pro Tip: In CJS, you get __dirname and __filename for free! 2. ES Modules (.mjs or "type": "module") The modern, official standard. It’s asynchronous, supports Top-Level Await, and allows for Tree Shaking (which keeps your bundles tiny). Keywords: import, export default, export const Best for: New projects, frontend-backend code sharing, and modern performance. Catch: No __dirname. You’ll need import.meta.url to find your path. 3. AMD (Asynchronous Module Definition) The "Blast from the Past." Mostly seen in browser-based legacy apps (like RequireJS). Keywords: define(['dep'], function(dep) { ... }) Status: Rarely used in modern Node.js development, but good to recognize in old codebases. 💡 Which one should you choose in 2026? If you're starting a new project: Go with ESM. Set "type": "module" in your package.json and embrace the future. It’s cleaner, faster, and the direction the entire JavaScript world is moving. How about you? Are you Team require or Team import? Let’s debate in the comments! 👇 #NodeJS #JavaScript #WebDev #Backend #Programming #CodingTips #SoftwareEngineering
To view or add a comment, sign in
-
Why #JavaScript Logic Confuses Even Developers 🧠💥 JavaScript is powerful... but it loves to surprise you. Here's why: Type Coercion Magic ✨ "11" + 1 // "111" "11" - 1 // 10 •means string concatenation •forces number conversion Same values, wildly different results! +"" // 0 +{} // NaN +[] // 0 Loose vs Strict Equality ⚖️ 0 == "0" // true 😱 0 === "0" // false ✅ Truthy & Falsy Surprises 🤯 Boolean("0") // true Boolean([]) // true Boolean({}) // true Boolean(0) // false JavaScript auto-converts types to be "helpful"... but that flexibility breeds bugs. How Pros Avoid This (React Dev Edition) 🚀 • Always use === over == • Convert explicitly: Number(), String(), Boolean() • Ditch implicit coercion • Switch to TypeScript for type safety in your React/Next.js apps JS isn't broken—it's just too flexible. Save this cheat sheet! 📌 Comment "JS" if you've been bitten by this. 👇 #JavaScript #WebDevelopment #ProgrammingHumor #CodingLife #Frontend #Developer #codingirlben #React #DeveloperLife #ReactJS #CodingMemes #DevHumor
To view or add a comment, sign in
-
-
JavaScript imports: Old vs Modern — what actually changed? If you started learning JavaScript a few years ago, you probably used require(). Today, most projects use import. At first glance, it looks like just a syntax change. In reality, the difference is much deeper. Here’s what changed. 1. Syntax and structure Old (CommonJS): const fs = require('fs'); module.exports = function greet() { console.log("Hello"); }; Modern (ES Modules): import fs from 'fs'; export function greet() { console.log("Hello"); } 2. When modules are loaded - CommonJS loads modules at runtime. - That means require() is executed when the code runs. - ES Modules are statically analyzed before execution. - The structure of imports and exports is known in advance. - This enables better tooling and optimization. 3. Synchronous vs asynchronous behavior CommonJS is synchronous by default. This works well in Node.js but doesn’t fit browsers perfectly. ES Modules are designed with asynchronous loading in mind and work natively in browsers. 4. Dynamic imports With CommonJS, you can require conditionally: if (condition) { const lib = require('./lib'); } With modern JavaScript, you use dynamic import: if (condition) { const lib = await import('./lib.js'); } 5. Performance and tree-shaking Because ES Modules are statically analyzable, bundlers can remove unused code (tree-shaking). With CommonJS, this is much harder. Why this matters ES Modules are now the standard for modern JavaScript — in browsers and in Node.js. They improve performance, tooling, and maintainability. CommonJS isn’t “wrong” — it’s just older. But for new projects, ES Modules are the better long-term choice. #JavaScript #ESModules #CommonJS #NodeJS #WebDevelopment #FrontendDevelopment #BackendDevelopment #SoftwareEngineering #Programming #Coding #WebDev #JSDeveloper #Tech #CleanCode
To view or add a comment, sign in
-
-
𝗡𝗼𝗱𝗲.𝗷𝘀 𝗡𝗼𝘁𝗲𝘀 | 𝗙𝗿𝗼𝗺 𝗕𝗮𝘀𝗶𝗰𝘀 𝘁𝗼 𝗣𝗿𝗼𝗱𝘂𝗰𝘁𝗶𝗼𝗻-𝗟𝗲𝘃𝗲𝗹 𝗖𝗼𝗻𝗰𝗲𝗽𝘁𝘀 Node.js isn’t just about writing JavaScript on the server — it’s about understanding how things work under the hood. These Node.js notes focus on the concepts that matter in real-world backend development and interviews. What these notes cover • Node.js Architecture & Event Loop • Single-Threaded & Non-Blocking I/O • Modules (CommonJS vs ES Modules) • NPM & Package Management • Express.js Basics & Middleware • REST APIs & HTTP Methods • Async Patterns (Callbacks, Promises, Async/Await) • Error Handling & Logging • Authentication & Authorization • Environment Variables & Config • Performance & Scaling Basics • Security Best Practices Useful for: Backend & Full-Stack interviews Building scalable APIs Writing clean, maintainable server code Tip: If you understand the event loop, Node.js stops feeling “magical”. #NodeJS #BackendDevelopment #JavaScript #FullStackDevelopment
To view or add a comment, sign in
-
This looks obvious… until it isn’t 👀 Most Node.js devs answer this too fast. No frameworks. No libraries. Just JavaScript fundamentals. Question 👇 function createCounter() { let count = 0; return { inc() { count++; }, get() { return count; } }; } const counter1 = createCounter(); const counter2 = createCounter(); counter1.inc(); counter1.inc(); counter2.inc(); console.log(counter1.get(), counter2.get()); ❓ What will be printed? A. 2 1 B. 3 3 C. 1 1 D. 2 2 👇 Drop ONE option only in the comments Why this matters Many developers know closures… but still get confused about: shared vs isolated state how function scope actually works why two “similar” objects behave differently When fundamentals aren’t clear: bugs feel random state becomes unpredictable debugging turns into guesswork Good engineers don’t just write code. They understand why it behaves this way. Did this one make you pause? 👀 #NodeJS #JavaScript #WebDevelopment #fullstackDeveloper #CodingChallenge #reactDeveloper 😊 #ProgrammingFundamentals #Closures #StateManagement #SoftwareEngineering #Debugging #DeveloperCommunity
To view or add a comment, sign in
-
-
Can You Predict the Output? If you’re working with JavaScript, understanding how this behaves is the difference between clean code and hours of debugging. 🧐 What’s the result? A) Hello B) undefined C) ReferenceError D) null Drop your answer in the comments before reading the explanation below! The Explanation : This is a classic "Context" trap. Here is why the answer is B) undefined: - The Outer Function: When obj.innerMessage() is called, this correctly points to obj. - The IIFE: Inside innerMessage, we have an Immediately Invoked Function Expression (IIFE). In non-strict mode, when a regular function is invoked like this (not as a method of an object), its this context defaults to the global object (window in browsers). - The Result: Since the global object doesn't have a property called message, it returns undefined. - The Bonus undefined: The final console.log(obj.innerMessage()) prints an extra undefined because innerMessage doesn't have a return statement! How to fix it? 🛠️ To get "Hello", you would use an Arrow Function for the IIFE, as arrow functions lexically bind this from the surrounding scope. #JavaScript #MERNStack #WebDevelopment #ReactJS #NodeJS #CodingChallenge #SoftwareEngineering #Hiring #FrontendDeveloper
To view or add a comment, sign in
-
-
🚨 𝐇𝐨𝐰 𝐭𝐨 𝐅𝐢𝐱 𝐓𝐲𝐩𝐞𝐒𝐜𝐫𝐢𝐩𝐭 𝐢𝐬𝐨𝐥𝐚𝐭𝐞𝐝𝐌𝐨𝐝𝐮𝐥𝐞𝐬 𝐄𝐫𝐫𝐨𝐫 𝐢𝐧 𝐍𝐞𝐱𝐭.𝐣𝐬? If your Next.js build fails with this message: Re-exporting a type when isolatedModules is enabled requires using export type Don’t worry, it’s not a complex bug. It’s just about being clear with TypeScript. 𝐖𝐡𝐚𝐭’𝐬 𝐀𝐜𝐭𝐮𝐚𝐥𝐥𝐲 𝐇𝐚𝐩𝐩𝐞𝐧𝐢𝐧𝐠? When `isolatedModules` is enabled, each file is compiled independently. So TypeScript must know exactly: - what is real JavaScript code that needed at runtime. - what is only for type checking. If you export a type like a normal value, the compiler expects it to exist in JavaScript but it doesn’t. That’s why the build fails. ❌ 𝐖𝐫𝐨𝐧𝐠 𝐖𝐚𝐲 export { Profile, saveProfile } from './helpers'; If `Profile` is a type and `saveProfile` is a function, this causes errors in isolated builds. ✅ 𝐂𝐨𝐫𝐫𝐞𝐜𝐭 𝐖𝐚𝐲 // runtime exports export { saveProfile } from './helpers'; // type-only exports export type { Profile } from './helpers'; Easy Rule to Remember: - Use `export {}` for functions, components, constants - Use `export type {}` for types and interfaces Have you encountered this issue? 😀 #creowis #TypeScript #NextJS #FrontendDev #EngineeringTips #WebDevelopment
To view or add a comment, sign in
-
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
Promise.all executes all promises in parallel, so all tasks hold memory at once. For heavy workloads, a sequential loop helps control server load. we encountered this issue at prod once .