写过前端的人都以为 INP 优化只能靠蒙——今天浏览器把哪个脚本慢了彻底公开了
写过前端的人都以为 INP 优化只能靠蒙——今天浏览器把哪个脚本慢了彻底公开了
页面卡了,你打开 Performance 面板,看到一个 200ms 的长任务——然后呢?
凭感觉猜是 React re-render 的问题?还是某个第三方脚本?这种事几乎每个写前端的人都干过。INP(Interaction to Next Paint)已经替代 FID 成为 Google 官方 Core Web Vital,目标是 200ms 以内,但优化 INP 的第一步——找出哪个脚本拖慢了交互——长期以来是个黑盒。Long Tasks API 只能告诉你「有任务慢了」,但具体是谁,怎么慢的,全靠工程师的经验和运气猜。
Chrome 123 开始,这件事被 PerformanceScriptTiming 彻底透明了。
它到底公开了什么
Long Animation Frames API(LoAF)比旧的 Long Tasks API 精确得多。每个 long-animation-frame 条目现在包含一个 scripts 数组,每个脚本元素携带:
- sourceURL — 精确到文件 URL,哪个 JS 文件
- sourceFunctionName — 函数名,哪个函数在执行
- sourceCharPosition — 字符位置,精确到行号级别
- invoker — 触发者,例如
DOMWindow.onclick或Window.setTimeout - invokerType — 触发者类型:event-listener / set-timeout / promise-callback / async-function 等
- forcedStyleAndLayoutDuration — 这个脚本触发了多少次强制同步布局/样式重算(也就是「布局抖动」时间)
- windowAttribution — 脚本跑在哪个 frame(top-level / iframe / same-page / descendant)
- pauseDuration — 脚本暂停时间,例如
Window.alert()或同步 XHR 阻塞的时间
加上 Duration 和 executionStart,你能精确还原:「这个脚本从第 10ms 开始执行,总共跑了 85ms,其中 23ms 是在做强制重排,它是被 #btn 的 click 事件触发调用的。」
这是 INP 优化史上第一次你能拿到如此精确的归因数据。
怎么拿到这些数据
用 PerformanceObserver 监听 long-animation-frame 条目:
const observer = new PerformanceObserver((list) => {
for (const entry of list.getEntries()) {
if (entry.duration > 50) {
entry.scripts.forEach(script => {
console.log("脚本:", {
sourceURL: script.sourceURL,
functionName: script.sourceFunctionName,
charPosition: script.sourceCharPosition,
invoker: script.invoker,
invokerType: script.invokerType,
duration: script.duration,
forcedStyleAndLayout: script.forcedStyleAndLayoutDuration,
pauseDuration: script.pauseDuration,
windowAttribution: script.windowAttribution,
});
});
}
}
});
observer.observe({ type: "long-animation-frame", buffered: true });
forcedStyleAndLayoutDuration 是重点。这个数字大于 0 就意味着你在读 DOM 布局属性(例如 offsetHeight、getBoundingClientRect)之前或之后写了 DOM——这就是经典的「布局抖动」(layout thrashing)。优化方法很简单:先把所有读取集中在一起,再把所有写入集中在一起。
Chrome DevTools 也接入了这些数据。打开 Performance 面板录制交互,找到 Interactions 轨道里超过 50ms 的任务(红色 Long Tasks),点进去,Main 轨道的 flame chart 里直接显示哪个脚本文件在执行,配合 invoker 信息就能定位到具体代码行。
配合 INP 三阶段看更清晰
INP 慢分三个阶段,现在 LoAF 脚本归因能直接对应到每个阶段:
- Input Delay(主线程被其他任务占着)— 看 blockingDuration,看哪个脚本占着主线程,配合 scheduler.yield() 分解
- Processing Duration(回调本身太重)— 看 duration 和 forcedStyleAndLayoutDuration,找 heavy event handler
- Presentation Delay(渲染太慢)— 看脚本有没有触发 forced reflow,以及 DOM 大小是否导致 paint 慢
forcedStyleAndLayoutDuration 特别有用:这个数字大于 0 就意味着代码在读 DOM 布局属性之前或之后写了 DOM,这是最常见的无意识性能杀手之一。修复方法是一句话:「先全部读,再全部写」。
实战怎么落地
第一步,打开 DevTools Performance 面板,录制一个你觉得慢的交互。找到 Interactions 轨道里红色标记的 Long Tasks,点击后在 Main 轨道的 flame chart 里找到具体文件,invoker 列会告诉你这是哪个事件的处理器。
第二步,把数据送到真实用户监控(RUM)。web-vitals 库 4.0+ 已经把 LoAF 数据集成进了 onINP 回调:
import { onINP } from "web-vitals/attribution";
onINP(({ value, attribution }) => {
if (attribution.longAnimationFrameEntries) {
attribution.longAnimationFrameEntries.forEach(loaf => {
loaf.scripts.forEach(script => {
sendBeacon("/api/perf", {
inpValue: value,
scriptURL: script.sourceURL,
functionName: script.sourceFunctionName,
duration: script.duration,
forcedLayout: script.forcedStyleAndLayoutDuration,
invoker: script.invoker,
});
});
});
}
});
有了这些数据,产品团队就能知道:真实用户的第 75 百分位 INP 问题是哪个第三方脚本造成的,而不是靠工程师对着 Lighthouse 报告猜。
浏览器支持情况
PerformanceScriptTiming 是 LoAF API 的一部分,Chrome 123(2024 年 3 月)稳定版开始支持,约 85% 全球覆盖率。Firefox Safari 暂不支持 script 级别的 attribution,仅支持 long-animation-frame 条目本身。建议用 @supports 做渐进增强:
if ("PerformanceScriptTiming" in window) {
// 使用脚本级归因
} else {
// 降级到只监听 long-animation-frame,做粗粒度监控
}
下一步行动:
打开 Chrome DevTools → Performance 面板,录制一次你觉得慢的交互,找到 Interactions 轨道里超过 50ms 的任务,查看 invoker 列。基本上每个人第一次用都会发现一个「没想到这个地方也会拖这么狠」的脚本。
评论区
登录后可评论。