2025-12-16 日报 Day304

Yuyang 前端小白🥬

今日的鸡汤

Fiber architecture and concurrency mode

Fiber可以解决以前的reconciler递归过程中阻塞主线程的问题
也没有办法处理高优先级的任务 例如用户输入,动画等

img

img

Fiber把一次“必须一口气做完的递归工作”,变成“可以被打断、恢复、调度的工作单元”

requestIdleCallback

requestIdleCallback is an experimental API that executes a callback when the browser is idle.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
((global: Window) => {
const id = 1;
const fps = 1e3/60;

let frameDeadline: number;
let pendingCallback: IdleRequestCallback;

const channel = new MessageChannel();
const timeRemaining = () => frameDeadline - window.performance.now();

const deadline = {
didTimeout: false,
timeRemaining
}

channel.port2.onmessage = () => {
if(typeof pendingCallback === 'function'){
pendingCallback(deadline);
}
}

global.requestIdleCallback = (callback: IdleRequestCallback) => {
global.requestAnimationFrame((frameTime) => {
frameDeadline = frameTime + fps;
pendingCallback = callback;
channel.port1.poseMessage(null);
});
return id;
}
})(window);

当你连续用 setTimeout(fn, 0) 做调度时,浏览器并不会真的 0ms 执行,而是会在一定次数后,强制每次至少延迟 4ms,因此它无法实现精确的时间切片。

workLoop -> performUnitOfWork -> reconcileChildren -> commitRoot.

评论
此页目录
2025-12-16 日报 Day304