💡 𝗜𝗻𝘁𝗲𝗿𝘃𝗶𝗲𝘄 𝗖𝗼𝗱𝗶𝗻𝗴 𝗤𝘂𝗲𝘀𝘁𝗶𝗼𝗻 (𝗝𝗮𝘃𝗮𝗦𝗰𝗿𝗶𝗽𝘁) 𝗤: 𝗛𝗼𝘄 𝘄𝗼𝘂𝗹𝗱 𝘆𝗼𝘂 𝗶𝗺𝗽𝗹𝗲𝗺𝗲𝗻𝘁 𝗮 𝘂𝘁𝗶𝗹𝗶𝘁𝘆 𝗳𝘂𝗻𝗰𝘁𝗶𝗼𝗻 𝘁𝗵𝗮𝘁 𝗺𝗲𝗮𝘀𝘂𝗿𝗲𝘀 𝘁𝗵𝗲 𝗲𝘅𝗲𝗰𝘂𝘁𝗶𝗼𝗻 𝘁𝗶𝗺𝗲 𝗼𝗳 𝗮 𝗳𝘂𝗻𝗰𝘁𝗶𝗼𝗻, 𝘀𝘂𝗽𝗽𝗼𝗿𝘁𝗶𝗻𝗴 𝗯𝗼𝘁𝗵 𝘀𝘆𝗻𝗰𝗵𝗿𝗼𝗻𝗼𝘂𝘀 𝗮𝗻𝗱 𝗮𝘀𝘆𝗻𝗰𝗵𝗿𝗼𝗻𝗼𝘂𝘀 𝗳𝘂𝗻𝗰𝘁𝗶𝗼𝗻𝘀? 𝗥𝗲𝗾𝘂𝗶𝗿𝗲𝗺𝗲𝗻𝘁𝘀: • Accept a function fn and its arguments • Log execution time in milliseconds • Return the result of fn • Work for both sync & async functions ✅ 𝗔𝗻𝘀𝘄𝗲𝗿: below ⚠️ 𝗪𝗵𝗲𝗿𝗲 𝗺𝗼𝘀𝘁 𝗰𝗮𝗻𝗱𝗶𝗱𝗮𝘁𝗲𝘀 𝗳𝗮𝗶𝗹: ❌ Not using finally → execution time not logged on errors ❌ Thinking return exits immediately and skips other code ❌ Writing separate logic for sync vs async functions 🧠 𝗜𝗻𝘁𝗲𝗿𝘃𝗶𝗲𝘄 𝗜𝗻𝘀𝗶𝗴𝗵𝘁: 𝗮𝘄𝗮𝗶𝘁 𝘄𝗼𝗿𝗸𝘀 𝗳𝗼𝗿 𝗯𝗼𝘁𝗵 𝘀𝘆𝗻𝗰 𝗮𝗻𝗱 𝗮𝘀𝘆𝗻𝗰 𝗳𝘂𝗻𝗰𝘁𝗶𝗼𝗻𝘀 𝗳𝗶𝗻𝗮𝗹𝗹𝘆 𝗮𝗹𝘄𝗮𝘆𝘀 𝗲𝘅𝗲𝗰𝘂𝘁𝗲𝘀, 𝗲𝘃𝗲𝗻 𝘄𝗵𝗲𝗻 𝗿𝗲𝘁𝘂𝗿𝗻 𝗶𝘀 𝗶𝗻𝘀𝗶𝗱𝗲 𝘁𝗿𝘆 📌 𝗘𝘅𝗮𝗺𝗽𝗹𝗲: measureExecutionTime(add, 2, 3); await measureExecutionTime(delay, 1000); 👉 𝗪𝗼𝘂𝗹𝗱 𝘆𝗼𝘂 𝘂𝘀𝗲 𝗗𝗮𝘁𝗲.𝗻𝗼𝘄() 𝗼𝗿 𝗽𝗲𝗿𝗳𝗼𝗿𝗺𝗮𝗻𝗰𝗲.𝗻𝗼𝘄() 𝗳𝗼𝗿 𝘁𝗵𝗶𝘀 𝗶𝗻 𝗽𝗿𝗼𝗱𝘂𝗰𝘁𝗶𝗼𝗻? #JavaScript #AsyncAwait #CodingInterview #Frontend #WebDevelopment #DevTips #InterviewPrep #interview #JavaScript #Promises #Frontend #WebDevelopment #CodingTips #InterviewPrep #CSS #Frontend #WebDevelopment #ReactJS #JavaScript #UI #FrontendTips #100DaysOfCode #HTML #FrontendDeveloper #LearnInPublic #ES6 #ModernJavaScript
JavaScript measure execution time for sync and async functions
More Relevant Posts
-
This async / await output confuses even experienced developers 🤯 🧩 JavaScript Output-Based Question (Async / Await) ✅ Correct Output 3 1 4 2 🧠 Why this output comes? (Step-by-Step) 1️⃣ Synchronous code runs first • console.log(3) → prints 3 2️⃣ test() is called • console.log(1) runs immediately → prints 1 3️⃣ await Promise.resolve() • Even though the promise is resolved, await pauses the function execution • Remaining code moves to the microtask queue 4️⃣ Back to synchronous code • console.log(4) → prints 4 5️⃣ Microtasks execute • console.log(2) runs last → prints 2 🔑 Key Takeaways (Interview Insight) ✔️ await is always asynchronous ✔️ Code after await runs in the microtask queue ✔️ Even resolved promises don’t run immediately ✔️ Understanding the event loop is critical for async JavaScript async / await looks synchronous, but behaves asynchronously. #JavaScript #AsyncAwait #InterviewQuestions #FrontendDeveloper #MERNStack #ReactJS
To view or add a comment, sign in
-
-
This async / await output confuses even experienced developers 🤯 🧩 JavaScript Output-Based Question (Async / Await) ✅ Correct Output 3 1 4 2 🧠 Why this output comes? (Step-by-Step) 1️⃣ Synchronous code runs first • console.log(3) → prints 3 2️⃣ test() is called • console.log(1) runs immediately → prints 1 3️⃣ await Promise.resolve() • Even though the promise is resolved, await pauses the function execution • Remaining code moves to the microtask queue 4️⃣ Back to synchronous code • console.log(4) → prints 4 5️⃣ Microtasks execute • console.log(2) runs last → prints 2 🔑 Key Takeaways (Interview Insight) ✔️ await is always asynchronous ✔️ Code after await runs in the microtask queue ✔️ Even resolved promises don’t run immediately ✔️ Understanding the event loop is critical for async JavaScript async / await looks synchronous, but behaves asynchronously. #JavaScript #AsyncAwait #InterviewQuestions #FrontendDeveloper #MERNStack #ReactJS
To view or add a comment, sign in
-
-
🤔 Ever wondered why arrow functions behave differently from normal functions in JavaScript? They look shorter, but they change the rules. 🧠 JavaScript interview question What are arrow functions and how are they different from normal functions? ✅ Short answer • Arrow functions are a shorter ES6 syntax • They do not have their own this • They are not constructors • They don’t have arguments, prototype, or new.target 🔍 A bit more detail 👉 Lexical this (the big one) Arrow functions capture this from where they are defined, not how they’re called. const obj = { count: 0, inc() { setTimeout(() => { this.count++; }, 100); } }; No .bind(this) needed. The arrow keeps the outer this. 👉 No own arguments Use rest parameters instead: const sum = (...nums) => nums.reduce((a, b) => a + b, 0); 👉 Not constructible new (() => {}) // ❌ TypeError Arrow functions have no prototype and can’t be used with new. 👉 Implicit returns const double = x => x * 2; Block body? You must return. ⚠️ When to avoid arrow functions • Object or prototype methods that rely on dynamic this • Constructors or generator functions • Event handlers that expect this to be the DOM element const obj = { total: 0, add() { this.total++; // ✅ correct } }; 🚀 When arrow functions shine • Callbacks (setTimeout, promises) • Array methods (map, filter, reduce) • Short utility functions • Preserving outer this in classes 🧩 Mental checklist • Need your own this? → normal function • Need outer this? → arrow function • Need new, arguments, or prototype? → normal function 💡 Arrow functions aren’t just shorter syntax, they change semantics. #javascript #frontend #webdevelopment #react #interviewprep #programming
To view or add a comment, sign in
-
💼 20 JavaScript Interview Questions You Should to Know Whether you're prepping for your first dev job or brushing up your fundamentals, these questions cover everything from core JS to modern syntax. ✅ Topics include: - `var` vs `let` vs `const` - Closures, hoisting, `this` - Async JS & the event loop - Arrow functions, deep vs shallow copy - Prototypes, functional programming - DOM events, delegation, and throttling - Destructuring, spread/rest, modules, and more 📦 The Complete Full-Stack Developer Roadmap ➡️ https://lnkd.in/gueMs7Fn If you found this guide helpful, follow TheDevSpace | Dev Roadmap for more tips, tutorials, and cheat sheets on web development. Let's stay connected! 🚀 Also follow 👉 W3Schools.com and JavaScript Mastery for more resources on web development. --- #JavaScript #WebDevelopment #TechInterview #CodingInterview #FrontendDev #100DaysOfCode #CodeNewbie #Nextjs #DevTips
To view or add a comment, sign in
-
🤔 JavaScript feels asynchronous. But it actually runs one thing at a time. That illusion is created by the event loop. 🧠 JavaScript interview question How does the event loop decide what runs next? ✅ Short answer • JavaScript runs on a single thread • Synchronous code runs first • Async work is scheduled via queues 🔍 A bit more detail • The call stack runs sync code top to bottom • When it’s empty, the event loop kicks in • It pulls work from queues in a strict order • Macrotasks - setTimeout - DOM events - I/O callbacks • Microtasks - Promise.then - async/await continuations - queueMicrotask • Microtasks always run before the next macrotask 🧪 Example console.log("A"); setTimeout(() => console.log("B"), 0); Promise.resolve().then(() => console.log("C")); console.log("D"); Output A D C B ⚠️ Small but important detail Microtasks are fully drained before rendering. Too many chained promises can block UI updates. That’s why async code can still freeze the screen. I’m posting one JavaScript concept every day to sharpen fundamentals and prep for interviews. Learning in public and staying consistent. #javascript #frontend #webdev #interviewprep
To view or add a comment, sign in
-
⏱️ JavaScript Timers – Understanding setTimeout Like a Pro setTimeout is one of the most commonly used async features in JavaScript – but many developers misunderstand how it really works. It does NOT execute exactly after the given time. It executes after the minimum delay + when the call stack is free. 🧠 Important Facts About setTimeout It is handled by the browser / Node timer API Callback goes to the Macrotask Queue Execution depends on the Event Loop 0 ms delay does NOT mean instant execution 🚀 Key Takeaways setTimeout is asynchronous Delay is the minimum wait time, not guaranteed time Even setTimeout(fn, 0) waits for: current code to finish event loop to pick it up 💡 Interview Insight If someone asks: “Why doesn’t setTimeout 0 run immediately?” Answer: 👉 Because JavaScript must finish synchronous code first, and timers run later through the event loop. If this clarified your understanding of timers, drop a 👍 #JavaScript #setTimeout #WebDevelopment #Frontend #Coding #InterviewPrep
To view or add a comment, sign in
-
-
🎨 Frontend development is getting ridiculously complex!! 🤔Remember when frontend was: HTML + CSS + JS 😌 🤯Now it’s: •Frameworks on frameworks •Build tools to manage build tools 🛠️ 10 dependencies for a button 😵💫 💬 Are we solving real problems… or creating them? #FrontendDevelopment #WebDev #JavaScript #OverEngineering #Founder #HTML #CSS #JS #Framework #Tools #Week3
To view or add a comment, sign in
-
Most JavaScript devs pause on this 👀 Even with async/await experience. No frameworks. No libraries. Just JavaScript fundamentals. Question 👇 async function test() { try { return Promise.reject("Error"); } catch (e) { console.log("caught"); } } test().catch(console.log); ❓ What will be printed to the console? A. caught B. Error C. Nothing D. Both caught and Error Why this matters Many developers assume: try/catch handles all errors inside async functions Promise.reject() behaves like throw That assumption is wrong. When fundamentals aren’t clear: error handling feels unpredictable bugs slip through silently debugging turns into guesswork Strong developers don’t guess. They understand how async functions actually propagate errors. 👇 Drop your answer in the comments Did this one make you think twice? #JavaScript #JSFundamentals #AsyncAwait #Promises #WebDevelopment #FrontendDeveloper #FullStackDeveloper #CodingInterview #DevelopersOfLinkedIn #DevCommunity #VibeCode
To view or add a comment, sign in
-
-
💡 Why Promises are better than Callbacks in JavaScript Early in my Node.js journey, my async code looked like this 👇 Callbacks inside callbacks inside callbacks… Debugging it? A nightmare 😵💫 That’s exactly the problem Promises were designed to solve. 🚫 The problem with callbacks • Deep nesting (callback hell) • Scattered error handling • Hard-to-read async flow • Poor scalability in large codebases ✅ Why Promises changed everything Promises give us: ✔ Clean chaining with .then() ✔ Centralized error handling using .catch() ✔ Better readability & maintainability ✔ Powerful utilities like Promise.all() ✔ Seamless support for async/await const user = await getUser(id); const orders = await getOrders(user.id); Async code that reads like synchronous code ✨ 🎯 Interview one-liner Promises solve callback hell by providing a cleaner async flow, better error handling, and improved readability, especially when used with async/await. If you’re working with Node.js or modern JavaScript, promises aren’t optional — they’re essential. 💬 Have you ever debugged callback hell in production? #JavaScript #NodeJS #BackendDevelopment #AsyncProgramming #WebDevelopment #Interviews #LearningInPublic
To view or add a comment, sign in
-
-
𝗝𝗮𝘃𝗮𝗦𝗰𝗿𝗶𝗽𝘁 𝗡𝗼𝘁𝗲𝘀 – 𝗖𝗼𝗺𝗽𝗹𝗲𝘁𝗲 𝗚𝘂𝗶𝗱𝗲 𝗳𝗿𝗼𝗺 𝗕𝗮𝘀𝗶𝗰𝘀 𝘁𝗼 𝗔𝗱𝘃𝗮𝗻𝗰𝗲𝗱 Well-structured JavaScript Notes designed to help you learn, revise, and master JavaScript for real-world development and technical interviews. These notes focus on core concepts, internal working, and practical usage — not just syntax. 🧠 Topics Covered ✅ JavaScript fundamentals & data types ✅ Scope, hoisting & execution context ✅ this keyword & binding (call / apply / bind) ✅ Closures & lexical environment ✅ Event loop, microtasks & macrotasks ✅ Promises, async / await & error handling ✅ Array & object methods (map, filter, reduce) ✅ Prototypes, inheritance & ES6+ features 🎯 Why these notes are useful • Clear explanations with real examples • Interview-oriented concepts & patterns • Helps debug real production issues • Perfect for quick revision before interviews 👨💻 Best for • Beginners & intermediate developers • Frontend & Full Stack engineers • JavaScript interview preparation • Anyone strengthening JS fundamentals 𝗜 𝗵𝗮𝘃𝗲 𝗽𝗿𝗲𝗽𝗮𝗿𝗲𝗱 𝗖𝗼𝗺𝗽𝗹𝗲𝘁𝗲 𝗜𝗻𝘁𝗲𝗿𝘃𝗶𝗲𝘄 𝗣𝗿𝗲𝗽𝗮𝗿𝗮𝘁𝗶𝗼𝗻 𝗚𝘂𝗶𝗱𝗲 𝗳𝗼𝗿 𝗙𝗿𝗼𝗻𝘁𝗲𝗻𝗱 𝗗𝗲𝘃𝗲𝗹𝗼𝗽𝗲𝗿. 𝗚𝗲𝘁 𝘁𝗵𝗲 𝗚𝘂𝗶𝗱𝗲 𝗵𝗲𝗿𝗲 👉 https://lnkd.in/dygKYGVx #JavaScript #JSNotes #WebDevelopment #FrontendDeveloper #FullStackDeveloper #InterviewPreparation #Programming
To view or add a comment, sign in
Explore related topics
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