Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is there a distinction between microtask and (macro)task in JavaScript?

Conceptually just having one queue for jobs seems to be sufficient for most use-cases.
What are the reasons for having multiple queues and distinguishing those into "microtasks" and (macro)"tasks"?

like image 947
james Avatar asked Jul 26 '21 07:07

james


People also ask

What are macro and micro tasks in JavaScript?

These are always the execution of the JavaScript code and micro-task queue is empty. Macro-task queue is often considered the same as the task queue or the event queue. However, the macro-task queue works the same as the task queue.

What is microtask in JavaScript?

A microtask is a short function which is executed after the function or program which created it exits and only if the JavaScript execution stack is empty, but before returning control to the event loop being used by the user agent to drive the script's execution environment.

What is microtask in Nodejs?

a Microtask. Microtasks are processed when the current task ends and the microtask queue is cleared before the next macrotask cycle. Microtasks can enqueue other microtasks. All are executed before the next task inline. UI rendering is run after all microtasks execution (NA for nodejs).

What is a Macrotask?

Macrotasking is a type of crowdsourcing that is distinct from microtasking. Macrotasks typically have the following characteristics: they can be done independently. they take a fixed amount of time. they require special skills.


Video Answer


1 Answers

Having multiple (macro) task queues allows for task prioritization.

For instance, an User Agent (UA) can choose to prioritize a user event (e.g a click event) over a network event, even if the latter was actually registered by the system first, because the former is probably more visible to the user and requires lower latency.
In HTML this is allowed by the first step of the Event Loop's Processing model, which states that the UA must choose the next task to execute from one of its task queues.
(note: HTML specs do not require that there are multiple queues, but they do define multiple task sources to guarantee the execution order of similar tasks).

Now, why have yet an other beast called microtask queue?

We can find a few early discussions about "how" it should be implemented, but I didn't dig as far as to find who proposed this idea the first and for what use case.

However from the discussions I found, we can see that a few proposals needing such a mechanism were on the road:

  • Mutation Observers
  • Now-deprecated ES Object.observe()
  • At-that-time-incoming ES Promises,
  • Also-incoming-at-that-time-and-I-don't-know-why-it's-cited ES WeakRefs
  • HTML Custom Elements callback

The first two being also the first to be implemented in browsers, we can probably say that their use case was the main reason for implementing this new kind of queue.

Both actually did similar things: they listened for changes on an object (or the DOM tree), and coalesced all the changes that occurred during a job into a single event (not to be read as Event).
One could argue that this event could have been queued in the next (macro) task, with the highest priority, except that the Event Loop was already a bit complex, and every jobs did not necessarily come from tasks.

For instance, the rendering is actually a part of every Event Loop iteration, except that most of the time it early exits because it wasn't the time to render.
So if you do your DOM modifications during a rendering frame, you could have the modifications rendered, and only after the whole rendering took place you'd get the callback.
Since the main use case for observers is to act on the observed changes before they trigger performance heavy side-effects, I guess you can see how it was necessary to have a mean to insert our callback right after the job which has done the modifications.


PS: At that time I didn't know much about the Event Loop, I was far from specs matters and I may have put some anachronisms in this answer.

like image 99
Kaiido Avatar answered Oct 16 '22 05:10

Kaiido