logologo
  • AI Tools

    DB Query GeneratorMock InterviewResume BuilderLearning Path GeneratorCheatsheet GeneratorAgentic Prompt GeneratorCompany ResearchCover Letter Generator
  • XpertoAI
  • MVP Ready
  • Resources

    CertificationsTopicsExpertsCollectionsArticlesQuestionsVideosJobs
logologo

Elevate Your Coding with our comprehensive articles and niche collections.

Useful Links

  • Contact Us
  • Privacy Policy
  • Terms & Conditions
  • Refund & Cancellation
  • About Us

Resources

  • Xperto-AI
  • Certifications
  • Python
  • GenAI
  • Machine Learning

Interviews

  • DSA
  • System Design
  • Design Patterns
  • Frontend System Design
  • ReactJS

Procodebase © 2024. All rights reserved.

Level Up Your Skills with Xperto-AI

A multi-AI agent platform that helps you level up your development skills and ace your interview preparation to secure your dream job.

Launch Xperto-AI

Exploring Micro tasks vs Macro tasks in JavaScript

author
Generated by
Abhishek Goyan

25/07/2024

JavaScript

Sign in to read full article

Introduction to Micro tasks and Macro tasks

In JavaScript, tasks can be broadly categorized into two types: micro tasks and macro tasks. Micro tasks are tasks that are executed after the current task has finished executing, while macro tasks are tasks that are queued in the event loop to be executed in the future.

Micro tasks

Micro tasks are tasks that are executed immediately after the current task has finished executing. Examples of micro tasks include promises, process.nextTick, and Object.observe. Micro tasks are executed before any other tasks, including macro tasks. This ensures that micro tasks are prioritized and executed as soon as possible.

console.log('Start'); Promise.resolve().then(() => console.log('Micro task')); console.log('End');

In the above example, the micro task will be executed before the 'End' log statement, even though it was defined after the 'End' statement.

Macro tasks

Macro tasks, on the other hand, are tasks that are queued in the event loop to be executed in the future. Examples of macro tasks include setTimeout, setInterval, and requestAnimationFrame. Macro tasks are executed after all micro tasks have been executed. This means that macro tasks have a lower priority compared to micro tasks.

console.log('Start'); setTimeout(() => console.log('Macro task'), 0); console.log('End');

In this example, the 'Macro task' log statement will be executed after the 'End' log statement, even though it was defined before the 'End' statement.

The Event Loop

The event loop is responsible for handling the execution of tasks in JavaScript. It keeps track of both micro tasks and macro tasks and ensures that they are executed in the appropriate order. The event loop continuously checks the task queue for any pending tasks and executes them accordingly.

Conclusion:

Overall, understanding the difference between micro tasks and macro tasks in JavaScript is essential for writing efficient and optimized code. By prioritizing micro tasks over macro tasks, you can ensure that critical tasks are executed as soon as possible. Next time you work with asynchronous JavaScript code, keep in mind the distinction between micro tasks and macro tasks to improve the performance of your code.

Popular Tags

JavaScriptMicro tasksMacro tasks

Share now!

Like & Bookmark!

Related Collections

  • JavaScript Coding Challenges for Interviews

    14/09/2024 | VanillaJS

  • JavaScript Interview Mastery: 20 Essential Concepts

    22/10/2024 | VanillaJS

  • JavaScript Mastery: From Basics to Advanced Techniques

    15/10/2024 | VanillaJS

Related Articles

  • Understanding JavaScript Scope Chain

    22/10/2024 | VanillaJS

  • Understanding Closures in JavaScript

    21/07/2024 | VanillaJS

  • Implementing an LRU Cache in JavaScript

    12/09/2024 | VanillaJS

  • Creating a Promise-Based API Request Function

    14/09/2024 | VanillaJS

  • Implementing a Debounce Function

    14/09/2024 | VanillaJS

  • Optimizing JavaScript Performance with a Memoization Function

    14/09/2024 | VanillaJS

  • Deep Cloning Object in JavaScript

    25/07/2024 | VanillaJS

Popular Category

  • Python
  • Generative AI
  • Machine Learning
  • ReactJS
  • System Design