Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

animation frame queue vs micro task queue

I guess that the microTask is processed in any steps.

And I tested these things, but I'm not sure that my assumption is right.

Test 1 - macroTask & microTask

for (let i = 0; i < 2; i += 1) {
  setTimeout(() => {
    console.log('-- macro --')
    queueMicrotask(() => console.log('** micro **'));
  });
}

results

-- macro --
** micro **
-- macro --
** micro **

Test 2 - microTask & animation frame

for (let i = 0; i < 3; i += 1) {
  requestAnimationFrame(() => {
    console.log('@@ animation callback @@');
    queueMicrotask(() => {
      console.log('** micro **');
    });
  });
}

results

@@ animation callback @@
** micro **
@@ animation callback @@
** micro **
@@ animation callback @@
** micro **

I was surprised when I first saw the results of this test. Because I thought that all tasks in the animation frame queue will be processed at once.

But, now I think that "while processing the animation frame callback functions, check if the microTaskQueue is empty, and if there is a task inside, process it, and call the next animation frame callback (inserted by requestAnimationFrame function."

Am I right, or am I missing something important?

like image 352
Byeongin Yoon Avatar asked Mar 12 '26 02:03

Byeongin Yoon


1 Answers

The microtask queue is visited every time the JS call stack is empty, as part of clean after running a script.

  1. If the JavaScript execution context stack is now empty, perform a microtask checkpoint.

This step is entered in between all the animation callback execution, from the invoke algorithm.

Note that the event-loop processing also has a few hard-coded microtask checkpoints, e.g at step 7, but also in other callbacks execution.

like image 94
Kaiido Avatar answered Mar 14 '26 18:03

Kaiido



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!