Skip to the code

The longest critical request chain

A critical request chain is a series of requests the page needs before it can render, where each one is found only once the one before it has arrived: the HTML names a stylesheet, the stylesheet imports another, and that one loads a font. Every link in the chain costs at least a round trip. Lighthouse's Network dependency tree insight draws these chains as a tree that starts at the document, and reports the longest as the maximum critical path latency. Write longestChain(requests), which finds it.

requests are a page load's critical requests, in the order they started; Lighthouse has already left out the rest, such as images, fetches, preloads, iframes, and requests that are neither high priority nor render-blocking. Each is an object with a url, an initiator (the URL of the request that led to it, or null), and start and end times in milliseconds. The first request whose initiator is null is the document. Any other request without an initiator, or whose initiator isn't in the list, belongs to no chain: Lighthouse drops requests with no initiator, and requests with a request that isn't critical anywhere above them.

Every request you can reach from the document by following initiators ends a chain: the requests from the document down to it. A chain's latency is its last request's end minus the document's start. Return { urls, latency } for the chain with the highest latency: its URLs, from the document down, and that latency. When two chains tie, take the one whose last request comes first in the list. Return null when there's no document.

longestChain([
  { url: "/menu", initiator: null, start: 0, end: 300 },
  { url: "/css/menu.css", initiator: "/menu", start: 320, end: 600 },
  { url: "/js/menu.js", initiator: "/menu", start: 330, end: 700 },
  { url: "/css/fonts.css", initiator: "/css/menu.css", start: 620, end: 800 },
  { url: "/fonts/lora.woff2", initiator: "/css/fonts.css", start: 820, end: 1100 },
]);
// { urls: ["/menu", "/css/menu.css", "/css/fonts.css", "/fonts/lora.woff2"], latency: 1100 }

What the tests check

  1. follows initiators from the document to the request that ends last
    const requests = [
      { url: "/menu", initiator: null, start: 0, end: 300 },
      { url: "/css/menu.css", initiator: "/menu", start: 320, end: 600 },
      { url: "/js/menu.js", initiator: "/menu", start: 330, end: 700 },
      { url: "/css/fonts.css", initiator: "/css/menu.css", start: 620, end: 800 },
      { url: "/fonts/lora.woff2", initiator: "/css/fonts.css", start: 820, end: 1100 },
    ];
    expect(longestChain(requests)).toEqual({ urls: ["/menu", "/css/menu.css", "/css/fonts.css", "/fonts/lora.woff2"], latency: 1100 });
    
  2. the longest chain in time isn't always the deepest
    const requests = [
      { url: "/menu", initiator: null, start: 0, end: 300 },
      { url: "/css/menu.css", initiator: "/menu", start: 320, end: 600 },
      { url: "/js/menu.js", initiator: "/menu", start: 330, end: 1500 },
      { url: "/css/fonts.css", initiator: "/css/menu.css", start: 620, end: 800 },
      { url: "/fonts/lora.woff2", initiator: "/css/fonts.css", start: 820, end: 1100 },
    ];
    expect(longestChain(requests)).toEqual({ urls: ["/menu", "/js/menu.js"], latency: 1500 });
    
  3. the document on its own is a chain
    expect(longestChain([{ url: "/menu", initiator: null, start: 0, end: 250 }])).toEqual({ urls: ["/menu"], latency: 250 });
    
  4. measures latency from the document's start
    const requests = [
      { url: "/menu", initiator: null, start: 120, end: 400 },
      { url: "/css/menu.css", initiator: "/menu", start: 410, end: 700 },
    ];
    expect(longestChain(requests)).toEqual({ urls: ["/menu", "/css/menu.css"], latency: 580 });
    
  5. a request with no initiator, or an initiator that isn't in the list, is in no chain
    const requests = [
      { url: "/menu", initiator: null, start: 0, end: 300 },
      { url: "/css/menu.css", initiator: "/menu", start: 320, end: 600 },
      { url: "/js/late-widget.js", initiator: null, start: 2000, end: 5000 },
      { url: "/js/chat.js", initiator: "/js/tag-manager.js", start: 2500, end: 6000 },
      { url: "/js/chat-ui.js", initiator: "/js/chat.js", start: 6100, end: 7000 },
    ];
    expect(longestChain(requests)).toEqual({ urls: ["/menu", "/css/menu.css"], latency: 600 });
    
  6. on a tie, the chain whose last request comes first in the list
    const requests = [
      { url: "/menu", initiator: null, start: 0, end: 300 },
      { url: "/js/menu.js", initiator: "/menu", start: 310, end: 900 },
      { url: "/css/menu.css", initiator: "/menu", start: 320, end: 500 },
      { url: "/css/fonts.css", initiator: "/css/menu.css", start: 510, end: 900 },
    ];
    expect(longestChain(requests)).toEqual({ urls: ["/menu", "/js/menu.js"], latency: 900 });
    
  7. without a document there's no chain
    expect(longestChain([])).toBe(null);
    expect(longestChain([{ url: "/css/menu.css", initiator: "/menu", start: 320, end: 600 }])).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).