Skip to content

This notebook belongs to

Sadat Jubayer

Senior Software Engineer at Premio

If found, please return to hello@jubayer.dev

Vol. 7Seven years of building

Started
January 2020
Everyday tools
TypeScript, Vue, React, Next.js, NestJS, React Native
Education
B.Sc. CSSE, AIUB, 2020
2 min readPerformance

Trace slow clicks with long animation frames

Use Chrome’s Long Animation Frames API to locate the script behind a slow click and separate script time from rendering work.

You click Filter on a results page. The count takes long enough to change that you can see the pause. Inspect the long frame around that click to see which scripts ran before the next paint.

A long animation frame records a frame that takes more than 50 ms, including time spent rendering if the browser rendered it. In this example, the click handler runs for about 75 ms before the browser can paint the changed count.

diagram

Work before the new results paint

The long frame includes both the filter handler and the rendering work after it.

Find the script on a reproduced click

Open the page with the slow filter, paste this into the DevTools console, then click Filter once. The observer keeps frames that report a UI event timestamp and sorts their recorded scripts by duration.

if (PerformanceObserver.supportedEntryTypes.includes("long-animation-frame")) {
  const observer = new PerformanceObserver((list) => {
    for (const frame of list.getEntries()) {
      if (!frame.firstUIEventTimestamp) continue
      const scripts = [...frame.scripts].sort((a, b) => b.duration - a.duration)
      console.log("Frame start and duration", frame.startTime, frame.duration)
      console.table(scripts.map((script) => ({
        callback: script.invoker,
        source: script.sourceURL,
        ms: Math.round(script.duration),
        forcedLayoutMs: Math.round(script.forcedStyleAndLayoutDuration),
      })))
    }
  })

  observer.observe({ type: "long-animation-frame" })
}

For the filter example, the table shows a row for filter.js with a click listener as its invoker.

Open that source file and look at the callback that runs for the click. If the forced style and layout column accounts for much of its duration, check for layout reads after DOM writes. If it is near zero, profile the work inside the callback before changing the layout code.

Match the frame to the interaction

The timestamp filter helps when you can reproduce a single click. Remove it to inspect frames from the same interaction that contain no UI event timestamp. On a busy page, compare the interaction's start and end times with each frame's start and end times. The work that delays an interaction can span frames, including a frame that started before the click handler ran.

An empty script table gives you no attribution to follow. The browser can omit work in a cross-origin iframe from the scripts array, for example. Record the same click in the Performance panel and inspect the time around it. If you need to tell input delay from handler time and presentation delay, split the interaction into its phases.