Skip to the code

Break down LCP

When LCP is slow, the first question is where the time went. Write lcpBreakdown(lcp, navigation, resources) that splits it into the four parts web-vitals 6.2.3's attribution build reports. lcp is the LCP entry, { startTime, url }, with url the image's address, or "" for text. navigation is the navigation entry (responseStart, and activationStart, which may be missing), and resources are the page's Resource Timing entries in order (name, the URL, startTime, requestStart, and responseEnd).

Return { value, timeToFirstByte, resourceLoadDelay, resourceLoadDuration, elementRenderDelay, lcpResourceEntry }. value is the LCP, startTime - activationStart (0 when missing), and never below 0. The LCP's resource is the last entry in resources whose name is the LCP's url and whose request started no later than the LCP's startTime; a request starts at requestStart, or at startTime when requestStart is 0, as it is for a cross-origin file without Timing-Allow-Origin. lcpResourceEntry is that entry, or null for text or when there's none.

Measure everything from activationStart. The first byte is responseStart, at least 0. The request start is the resource's request time, but never before the first byte (the first byte itself when there's no resource). The response end is the resource's responseEnd, but never before the request start and never after value (the request start when there's no resource). timeToFirstByte is the first byte, resourceLoadDelay runs from it to the request start, resourceLoadDuration from the request start to the response end, and elementRenderDelay from the response end to value, so the four add up to value.

web-vitals ignores a navigation entry whose responseStart is 0 or less. Then activationStart counts as 0, the first three parts are 0, and all of value is render delay, but the resource is still found.

lcpBreakdown(
  { startTime: 2100, url: "https://leafandkettle.test/teas/oolong.avif" },
  { responseStart: 400, activationStart: 0 },
  [{ name: "https://leafandkettle.test/teas/oolong.avif", startTime: 900, requestStart: 950, responseEnd: 1700 }],
);
// { value: 2100, timeToFirstByte: 400, resourceLoadDelay: 550, resourceLoadDuration: 750,
//   elementRenderDelay: 400, lcpResourceEntry: the oolong entry }

What the tests check

  1. an image's LCP splits into four parts that add up to it
    const perf = usePerformance();
    const url = "https://leafandkettle.test/teas/oolong.avif";
    const navigation = perf.navigation({ responseStart: 400 });
    perf.buffer("resource", [
      { name: "https://leafandkettle.test/shop.css", initiatorType: "link", startTime: 420, requestStart: 430, responseEnd: 600 },
      { name: url, initiatorType: "img", startTime: 900, requestStart: 950, responseEnd: 1700 },
    ]);
    const resources = performance.getEntriesByType("resource");
    const result = lcpBreakdown({ startTime: 2100, url }, navigation, resources);
    expect(result).toEqual({ value: 2100, timeToFirstByte: 400, resourceLoadDelay: 550, resourceLoadDuration: 750, elementRenderDelay: 400, lcpResourceEntry: resources[1] });
    expect(result.lcpResourceEntry).toBe(resources[1]);
    
  2. text has no resource, so everything after the first byte is render delay
    const resources = [{ name: "https://leafandkettle.test/shop.css", startTime: 420, requestStart: 430, responseEnd: 600 }];
    expect(lcpBreakdown({ startTime: 1300, url: "" }, { responseStart: 400 }, resources)).toEqual({ value: 1300, timeToFirstByte: 400, resourceLoadDelay: 0, resourceLoadDuration: 0, elementRenderDelay: 900, lcpResourceEntry: null });
    
  3. only a request for the LCP's exact URL counts
    const resources = [{ name: "https://leafandkettle.test/teas/oolong-480.avif", startTime: 900, requestStart: 950, responseEnd: 1700 }];
    const result = lcpBreakdown({ startTime: 2100, url: "https://leafandkettle.test/teas/oolong.avif" }, { responseStart: 400, activationStart: 0 }, resources);
    expect(result).toEqual({ value: 2100, timeToFirstByte: 400, resourceLoadDelay: 0, resourceLoadDuration: 0, elementRenderDelay: 1700, lcpResourceEntry: null });
    
  4. a cross-origin image without Timing-Allow-Origin is timed from its startTime
    const url = "https://images.example.test/teas/mint.avif";
    const image = { name: url, startTime: 700, requestStart: 0, responseEnd: 1500 };
    expect(lcpBreakdown({ startTime: 1800, url }, { responseStart: 400, activationStart: 0 }, [image])).toEqual({ value: 1800, timeToFirstByte: 400, resourceLoadDelay: 300, resourceLoadDuration: 800, elementRenderDelay: 300, lcpResourceEntry: image });
    
  5. uses the last request for the URL that started before the LCP painted
    const url = "https://leafandkettle.test/teas/green.avif";
    const first = { name: url, startTime: 500, requestStart: 520, responseEnd: 900 };
    const second = { name: url, startTime: 1200, requestStart: 1250, responseEnd: 1500 };
    const third = { name: url, startTime: 1900, requestStart: 1950, responseEnd: 2300 };
    const other = { name: "https://leafandkettle.test/app.js", startTime: 1300, requestStart: 1310, responseEnd: 1400 };
    const result = lcpBreakdown({ startTime: 1600, url }, { responseStart: 400, activationStart: 0 }, [first, second, other, third]);
    expect(result).toEqual({ value: 1600, timeToFirstByte: 400, resourceLoadDelay: 850, resourceLoadDuration: 250, elementRenderDelay: 100, lcpResourceEntry: second });
    expect(result.lcpResourceEntry).toBe(second);
    
  6. no part is negative: an early request has no load delay, and a long download no render delay
    const url = "https://leafandkettle.test/teas/oolong.avif";
    // Preloaded from a 103 Early Hints response, before the HTML's first byte.
    const hinted = { name: url, startTime: 150, requestStart: 160, responseEnd: 1000 };
    expect(lcpBreakdown({ startTime: 1400, url }, { responseStart: 400 }, [hinted])).toEqual({ value: 1400, timeToFirstByte: 400, resourceLoadDelay: 0, resourceLoadDuration: 600, elementRenderDelay: 400, lcpResourceEntry: hinted });
    // Downloaded completely before the first byte.
    const done = { name: url, startTime: 100, requestStart: 110, responseEnd: 300 };
    expect(lcpBreakdown({ startTime: 1400, url }, { responseStart: 400 }, [done])).toEqual({ value: 1400, timeToFirstByte: 400, resourceLoadDelay: 0, resourceLoadDuration: 0, elementRenderDelay: 1000, lcpResourceEntry: done });
    // A video that keeps downloading after its first frame is the LCP.
    const video = { name: "https://leafandkettle.test/brewing.mp4", startTime: 780, requestStart: 800, responseEnd: 5000 };
    expect(lcpBreakdown({ startTime: 2600, url: video.name }, { responseStart: 400 }, [video])).toEqual({ value: 2600, timeToFirstByte: 400, resourceLoadDelay: 400, resourceLoadDuration: 1800, elementRenderDelay: 0, lcpResourceEntry: video });
    
  7. a prerendered page measures every part from activationStart
    const url = "https://leafandkettle.test/teas/oolong.avif";
    const image = { name: url, startTime: 780, requestStart: 800, responseEnd: 1400 };
    expect(lcpBreakdown({ startTime: 1600, url }, { responseStart: 300, activationStart: 1000 }, [image])).toEqual({ value: 600, timeToFirstByte: 0, resourceLoadDelay: 0, resourceLoadDuration: 400, elementRenderDelay: 200, lcpResourceEntry: image });
    const ready = { name: url, startTime: 480, requestStart: 500, responseEnd: 850 };
    expect(lcpBreakdown({ startTime: 900, url }, { responseStart: 300, activationStart: 1000 }, [ready])).toEqual({ value: 0, timeToFirstByte: 0, resourceLoadDelay: 0, resourceLoadDuration: 0, elementRenderDelay: 0, lcpResourceEntry: ready });
    
  8. a navigation entry without a responseStart is ignored
    const url = "https://leafandkettle.test/teas/oolong.avif";
    const image = { name: url, startTime: 900, requestStart: 950, responseEnd: 1700 };
    expect(lcpBreakdown({ startTime: 2100, url }, { responseStart: 0, activationStart: 500 }, [image])).toEqual({ value: 2100, timeToFirstByte: 0, resourceLoadDelay: 0, resourceLoadDuration: 0, elementRenderDelay: 2100, lcpResourceEntry: image });
    

Learn more

solution.jsSaved in this browser

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).