A long animation frame is a frame that took more than 50 ms from the start of its first task to the end of its rendering, or a task over 50 ms that didn't render at all, and Chrome reports each as a long-animation-frame entry. Write watchLongFrames(options), which watches them and returns { report, stop }, so a page can send home its worst frames and the scripts behind them, as Chrome for Developers' guide to the Long Animation Frames API suggests.
Each entry has a startTime and a duration (not counting presenting the frame); a blockingDuration, how long the frame kept input waiting (for each of its tasks over 50 ms, the part past 50 ms, with the rendering counted in the longest task); renderStart, when rendering began, or 0 for a frame that didn't render; styleAndLayoutStart, when style and layout began, or 0; and scripts, one object for each script that ran for more than 5 ms, with its duration, its invoker (how it was called, like "BUTTON#add.onclick"), and its sourceURL (the file it came from, or "" when that isn't known).
Observe { type: "long-animation-frame", buffered: true } with one PerformanceObserver. Where PerformanceObserver.supportedEntryTypes doesn't list the type, observe nothing, and report() gives empty lists. stop() disconnects the observer.
report() first handles the observer's takeRecords(), so frames not yet delivered are in it, and returns { frames, scripts }. frames are the options.keep frames (10 when it isn't given) with the highest blockingDuration, highest first, the earlier frame first on a tie. Each is a plain object { startTime, duration, blockingDuration, workDuration, renderDuration, styleAndLayoutDuration }, by the guide's formulas: workDuration is renderStart - startTime, or the whole duration when renderStart is 0; renderDuration is startTime + duration - renderStart, or 0 when renderStart is 0; styleAndLayoutDuration is startTime + duration - styleAndLayoutStart, or 0 when styleAndLayoutStart is 0.
scripts covers every frame seen, not only the ones kept: one row per sourceURL, { sourceURL, count, totalDuration, invoker }, with how many scripts came from that file, their durations added up, and the invoker of its longest script (the first one, on a tie). Sort the rows by totalDuration, highest first; rows that tie stay in the order their files first appeared.
const longFrames = watchLongFrames();
addEventListener("visibilitychange", () => {
if (document.visibilityState === "hidden") navigator.sendBeacon("/analytics", JSON.stringify(longFrames.report()));
});
// A tap on "Add to basket" ran 60 ms of menu.js, and the frame rendered 20 ms later:
// frames: [{ startTime: 1000, duration: 140, blockingDuration: 40, workDuration: 120, renderDuration: 20, styleAndLayoutDuration: 10 }]
// scripts: [{ sourceURL: "https://leafandkettle.example/js/menu.js", count: 1, totalDuration: 60, invoker: "BUTTON#add.onclick" }]
const perf = usePerformance();
const frame = (startTime, duration, blockingDuration, renderStart, styleAndLayoutStart, scripts = []) => ({ startTime, duration, blockingDuration, renderStart, styleAndLayoutStart, scripts });
perf.buffer("long-animation-frame", [frame(1000, 140, 40, 1120, 1130)]);
const longFrames = watchLongFrames();
expect(perf.observers().map((observer) => [observer.types, observer.buffered])).toEqual([[["long-animation-frame"], true]]);
await flush();
perf.emit("long-animation-frame", [frame(3000, 90, 40, 0, 0), frame(5000, 64, 0, 5040, 0)]);
expect(longFrames.report().frames).toEqual([
{ startTime: 1000, duration: 140, blockingDuration: 40, workDuration: 120, renderDuration: 20, styleAndLayoutDuration: 10 },
{ startTime: 3000, duration: 90, blockingDuration: 40, workDuration: 90, renderDuration: 0, styleAndLayoutDuration: 0 },
{ startTime: 5000, duration: 64, blockingDuration: 0, workDuration: 40, renderDuration: 24, styleAndLayoutDuration: 0 },
]);
const perf = usePerformance();
const frame = (startTime, duration, blockingDuration, renderStart, styleAndLayoutStart, scripts = []) => ({ startTime, duration, blockingDuration, renderStart, styleAndLayoutStart, scripts });
const longFrames = watchLongFrames();
const blocking = [30, 120, 0, 85, 120, 45, 10, 60, 200, 5, 75, 20];
const frames = blocking.map((blockingDuration, index) => frame(1000 * (index + 1), 60 + blockingDuration, blockingDuration, 1000 * (index + 1) + 50, 0));
perf.emit("long-animation-frame", frames.slice(0, 4));
perf.emit("long-animation-frame", frames.slice(4, 8));
perf.emit("long-animation-frame", frames.slice(8));
expect(longFrames.report().frames.map((kept) => [kept.startTime, kept.blockingDuration])).toEqual([
[9000, 200],
[2000, 120],
[5000, 120],
[4000, 85],
[11000, 75],
[8000, 60],
[6000, 45],
[1000, 30],
[12000, 20],
[7000, 10],
]);
const perf = usePerformance();
const frame = (startTime, duration, blockingDuration, renderStart, styleAndLayoutStart, scripts = []) => ({ startTime, duration, blockingDuration, renderStart, styleAndLayoutStart, scripts });
const script = (sourceURL, invoker, duration) => ({ sourceURL, invoker, invokerType: "event-listener", duration });
const menu = "https://leafandkettle.example/js/menu.js";
const chat = "https://widgets.example.com/chat.js";
const photos = "https://leafandkettle.example/js/photos.js";
const longFrames = watchLongFrames();
perf.emit("long-animation-frame", [
frame(1000, 160, 90, 1130, 1140, [script(menu, "BUTTON#add.onclick", 60), script(chat, "TimerHandler:setTimeout", 40)]),
frame(4000, 120, 50, 4100, 4105, [script(chat, "Window.requestAnimationFrame", 70), script("", "Response.json.then", 12)]),
frame(6000, 110, 40, 6090, 6095, [script(menu, "INPUT#search.oninput", 60), script(photos, "IMG#oolong.onload", 12), script(chat, "TimerHandler:setTimeout", 20)]),
]);
expect(longFrames.report().scripts).toEqual([
{ sourceURL: chat, count: 3, totalDuration: 130, invoker: "Window.requestAnimationFrame" },
{ sourceURL: menu, count: 2, totalDuration: 120, invoker: "BUTTON#add.onclick" },
{ sourceURL: "", count: 1, totalDuration: 12, invoker: "Response.json.then" },
{ sourceURL: photos, count: 1, totalDuration: 12, invoker: "IMG#oolong.onload" },
]);
const perf = usePerformance();
const frame = (startTime, duration, blockingDuration, renderStart, styleAndLayoutStart, scripts = []) => ({ startTime, duration, blockingDuration, renderStart, styleAndLayoutStart, scripts });
const script = (sourceURL, invoker, duration) => ({ sourceURL, invoker, invokerType: "user-callback", duration });
const menu = "https://leafandkettle.example/js/menu.js";
const chat = "https://widgets.example.com/chat.js";
const longFrames = watchLongFrames({ keep: 1 });
perf.emit("long-animation-frame", [
frame(1000, 120, 70, 1100, 1105, [script(menu, "BUTTON#add.onclick", 60)]),
frame(3000, 200, 150, 3180, 3190, [script(chat, "TimerHandler:setTimeout", 140)]),
]);
const { frames, scripts } = longFrames.report();
expect(frames.map((kept) => kept.startTime)).toEqual([3000]);
expect(scripts.map((row) => [row.sourceURL, row.totalDuration])).toEqual([[chat, 140], [menu, 60]]);
const perf = usePerformance();
const menu = "https://leafandkettle.example/js/menu.js";
perf.buffer("long-animation-frame", [
{ startTime: 1000, duration: 140, blockingDuration: 40, renderStart: 1120, styleAndLayoutStart: 1130, scripts: [{ sourceURL: menu, invoker: "BUTTON#add.onclick", duration: 60 }] },
]);
const longFrames = watchLongFrames();
const early = longFrames.report(); // before the buffered frame has been delivered
expect(early.frames.map((kept) => kept.startTime)).toEqual([1000]);
expect(early.scripts).toEqual([{ sourceURL: menu, count: 1, totalDuration: 60, invoker: "BUTTON#add.onclick" }]);
await flush();
expect(longFrames.report().scripts).toEqual([{ sourceURL: menu, count: 1, totalDuration: 60, invoker: "BUTTON#add.onclick" }]);
const perf = usePerformance();
const longFrames = watchLongFrames();
longFrames.stop();
expect(perf.observers()[0].disconnected).toBe(true);
perf.emit("long-animation-frame", [{ startTime: 1000, duration: 140, blockingDuration: 40, renderStart: 1120, styleAndLayoutStart: 1130, scripts: [] }]);
expect(longFrames.report()).toEqual({ frames: [], scripts: [] });
const perf = usePerformance();
perf.supported(["event", "first-input", "largest-contentful-paint", "layout-shift", "longtask", "mark", "measure", "navigation", "paint", "resource"]);
const longFrames = watchLongFrames();
expect(perf.observers()).toEqual([]);
expect(longFrames.report()).toEqual({ frames: [], scripts: [] });
longFrames.stop();
Run tests runs every test on your code, here in your browser. The problem is solved when they all pass.
Ctrl + Enter runs the tests (⌘ + Enter on a Mac).
I'm here to help with Report the worst long animation frames. I give hints, never the solution, so you get to solve it yourself. Ask for a hint, select code to ask about it, or press Ask Tuto why on a failing test.
Your first 3 answers need no account. for free to keep asking.