Skip to the code

Read a CrUX histogram

The CrUX API (Chrome UX Report) answers with a histogram for each metric of a page or an origin: three bins, good, needs improvement, and poor, each { start, end, density }. density is the share of page loads in the bin, rounded to 4 decimal places, so the three add up to about 1 rather than exactly 1. The last bin has no end: it runs from start up. start and end are numbers for timings, in ms, and strings for CLS, which the API sends as text with two decimals ("0.10"). Write cruxHistogram(histogram), which returns { good, needsImprovement, poor, p75 }.

The three shares are the bins' densities as given. p75 is the bin that holds the 75th percentile: the first bin, from good up, where the running total of densities reaches three quarters of the sum of all three (at or above it). Return it as { rating, start, end }, where rating is "good", "needs-improvement", or "poor", start and end are numbers, and end is null for the last bin. Return null when the densities add up to 0.

cruxHistogram([
  { start: 0, end: 2500, density: 0.6812 }, // the tea shop's LCP on phones
  { start: 2500, end: 4000, density: 0.2104 },
  { start: 4000, density: 0.1084 },
]);
// 0.6812 is short of three quarters; 0.6812 + 0.2104 = 0.8916 reaches it
// { good: 0.6812, needsImprovement: 0.2104, poor: 0.1084, p75: { rating: "needs-improvement", start: 2500, end: 4000 } }

What the tests check

  1. p75 is in good when at least three quarters of loads are good
    const histogram = [{ start: 0, end: 2500, density: 0.8123 }, { start: 2500, end: 4000, density: 0.1209 }, { start: 4000, density: 0.0668 }];
    expect(cruxHistogram(histogram)).toEqual({ good: 0.8123, needsImprovement: 0.1209, poor: 0.0668, p75: { rating: "good", start: 0, end: 2500 } });
    
  2. p75 moves up to needs improvement when good falls short
    const histogram = [{ start: 0, end: 2500, density: 0.6812 }, { start: 2500, end: 4000, density: 0.2104 }, { start: 4000, density: 0.1084 }];
    expect(cruxHistogram(histogram)).toEqual({ good: 0.6812, needsImprovement: 0.2104, poor: 0.1084, p75: { rating: "needs-improvement", start: 2500, end: 4000 } });
    
  3. p75 is in poor, the bin with no end, when the first two fall short together
    const histogram = [{ start: 0, end: 200, density: 0.52 }, { start: 200, end: 500, density: 0.21 }, { start: 500, density: 0.27 }];
    expect(cruxHistogram(histogram)).toEqual({ good: 0.52, needsImprovement: 0.21, poor: 0.27, p75: { rating: "poor", start: 500, end: null } });
    const allPoor = [{ start: 0, end: 200, density: 0 }, { start: 200, end: 500, density: 0 }, { start: 500, density: 1 }];
    expect(cruxHistogram(allPoor).p75).toEqual({ rating: "poor", start: 500, end: null });
    
  4. exactly three quarters is enough
    const fcp = [{ start: 0, end: 1800, density: 0.75 }, { start: 1800, end: 3000, density: 0.15 }, { start: 3000, density: 0.1 }];
    expect(cruxHistogram(fcp).p75).toEqual({ rating: "good", start: 0, end: 1800 });
    const ttfb = [{ start: 0, end: 800, density: 0.6 }, { start: 800, end: 1800, density: 0.15 }, { start: 1800, density: 0.25 }];
    expect(cruxHistogram(ttfb).p75).toEqual({ rating: "needs-improvement", start: 800, end: 1800 });
    
  5. measures against the densities' sum, which isn't always exactly 1
    const under = [{ start: 0, end: 2500, density: 0.7499 }, { start: 2500, end: 4000, density: 0.15 }, { start: 4000, density: 0.0999 }];
    expect(cruxHistogram(under).p75.rating).toBe("good");
    const over = [{ start: 0, end: 2500, density: 0.7501 }, { start: 2500, end: 4000, density: 0.15 }, { start: 4000, density: 0.1001 }];
    expect(cruxHistogram(over).p75.rating).toBe("needs-improvement");
    
  6. CLS bins come as strings and go back as numbers
    const cls = [{ start: "0.00", end: "0.10", density: 0.62 }, { start: "0.10", end: "0.25", density: 0.25 }, { start: "0.25", density: 0.13 }];
    expect(cruxHistogram(cls)).toEqual({ good: 0.62, needsImprovement: 0.25, poor: 0.13, p75: { rating: "needs-improvement", start: 0.1, end: 0.25 } });
    
  7. no page loads is null
    const empty = [{ start: 0, end: 2500, density: 0 }, { start: 2500, end: 4000, density: 0 }, { start: 4000, density: 0 }];
    expect(cruxHistogram(empty)).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).