Skip to the code

Break down TTFB

Time to First Byte is how long the page waited for the first byte of its HTML. Write ttfbBreakdown(entry). entry is the page's Navigation Timing entry (performance.getEntriesByType("navigation")[0]), with times in ms from the start of the navigation such as fetchStart, workerStart, domainLookupStart, connectStart, connectEnd, requestStart, and responseStart, and, in Chromium browsers, activationStart. Return TTFB and the parts web-vitals 6.2.3's attribution build reports, as { value, waitingDuration, cacheDuration, dnsDuration, connectionDuration, requestDuration }.

A prerendered page is measured from when the user opened it, activationStart (missing or 0 for a normal load). Subtract it from every time you use, and turn anything below 0 into 0. Then value is responseStart. The waiting ends at workerStart when a service worker started, and at fetchStart otherwise (workerStart is 0 without one). waitingDuration runs from 0 to the end of the waiting (redirects happen here), cacheDuration from there to domainLookupStart (a service worker and the HTTP cache), dnsDuration from domainLookupStart to connectStart, connectionDuration from connectStart to connectEnd, and requestDuration from connectEnd to responseStart. The five parts add up to value.

Return null when responseStart is 0 or less: browsers report 0 when they won't say, and web-vitals then reports no TTFB.

ttfbBreakdown(performance.getEntriesByType("navigation")[0]);
// The tea shop's home page, over a new connection:
// fetchStart 4, domainLookupStart 12, connectStart 40, connectEnd 128, responseStart 410
// { value: 410, waitingDuration: 4, cacheDuration: 8, dnsDuration: 28, connectionDuration: 88, requestDuration: 282 }

What the tests check

  1. TTFB and its parts for a page loaded over a new connection
    const perf = usePerformance();
    const entry = perf.navigation({ fetchStart: 4, domainLookupStart: 12, domainLookupEnd: 40, connectStart: 40, secureConnectionStart: 70, connectEnd: 128, requestStart: 130, responseStart: 410, responseEnd: 480 });
    expect(ttfbBreakdown(entry)).toEqual({ value: 410, waitingDuration: 4, cacheDuration: 8, dnsDuration: 28, connectionDuration: 88, requestDuration: 282 });
    
  2. the five parts add up to TTFB
    const perf = usePerformance();
    const entry = perf.navigation({ fetchStart: 9, domainLookupStart: 15, connectStart: 61, connectEnd: 203, requestStart: 211, responseStart: 977 });
    const result = ttfbBreakdown(entry);
    expect(result.value).toBe(977);
    const parts = result.waitingDuration + result.cacheDuration + result.dnsDuration + result.connectionDuration + result.requestDuration;
    expect(parts).toBe(977);
    expect(result.requestDuration).toBe(774);
    
  3. a redirect is part of the waiting
    const perf = usePerformance();
    const entry = perf.navigation({ redirectStart: 2, redirectEnd: 180, redirectCount: 1, fetchStart: 182, domainLookupStart: 182, connectStart: 182, connectEnd: 182, requestStart: 184, responseStart: 350 });
    expect(ttfbBreakdown(entry)).toEqual({ value: 350, waitingDuration: 182, cacheDuration: 0, dnsDuration: 0, connectionDuration: 0, requestDuration: 168 });
    
  4. a service worker's start-up and work count as cache time
    const perf = usePerformance();
    const entry = perf.navigation({ workerStart: 20, fetchStart: 45, domainLookupStart: 45, connectStart: 45, connectEnd: 45, requestStart: 46, responseStart: 140 });
    expect(ttfbBreakdown(entry)).toEqual({ value: 140, waitingDuration: 20, cacheDuration: 25, dnsDuration: 0, connectionDuration: 0, requestDuration: 95 });
    
  5. a prerendered page is measured from activationStart, and no part is below 0
    const perf = usePerformance();
    const times = { fetchStart: 4, domainLookupStart: 12, connectStart: 40, connectEnd: 128, requestStart: 130, responseStart: 410 };
    expect(ttfbBreakdown(perf.navigation({ ...times, activationStart: 50 }))).toEqual({ value: 360, waitingDuration: 0, cacheDuration: 0, dnsDuration: 0, connectionDuration: 78, requestDuration: 282 });
    expect(ttfbBreakdown(perf.navigation({ ...times, activationStart: 1500 }))).toEqual({ value: 0, waitingDuration: 0, cacheDuration: 0, dnsDuration: 0, connectionDuration: 0, requestDuration: 0 });
    
  6. an entry without activationStart, as in Safari and Firefox, is measured from 0
    const entry = { entryType: "navigation", fetchStart: 3, workerStart: 0, domainLookupStart: 3, connectStart: 3, connectEnd: 3, requestStart: 5, responseStart: 220 };
    expect(ttfbBreakdown(entry)).toEqual({ value: 220, waitingDuration: 3, cacheDuration: 0, dnsDuration: 0, connectionDuration: 0, requestDuration: 217 });
    
  7. a responseStart of 0 means there's no TTFB to report
    const perf = usePerformance();
    expect(ttfbBreakdown(perf.navigation({ fetchStart: 4, responseStart: 0 }))).toBe(null);
    

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