Skip to the code

The 75th percentile of field data

Field data is a pile of samples: one LCP, INP, or CLS value from each real visit to a page. Write fieldSummary(samples, threshold). samples is an array of numbers (milliseconds, or scores for CLS), and threshold is the metric's good threshold, such as 2500 for LCP. Return { p75, goodShare, passes }: the 75th percentile of the samples, the share of them that are good (from 0 to 1), and whether the 75th percentile is good.

Google assesses a page's Core Web Vitals at the 75th percentile of its visits, and the Chrome UX Report (CrUX) publishes each metric's 75th percentile. Use the nearest-rank rule: sort the samples from smallest to largest, and the 75th percentile is the one at position Math.ceil(0.75 * n), counting from 1, where n is how many samples there are. Of 4 samples it's the 3rd smallest, of 10 the 8th, and of 1 the only one. Leave the array you were given in its order.

A sample is good when it's at or below threshold (web-vitals rates a value right on a threshold as good), so goodShare is the number of good samples divided by n. passes is true when p75 is at or below threshold. Return null when there are no samples.

// The LCP of eight visits to the tea shop's home page, in ms
fieldSummary([2300, 1900, 4100, 2600, 1200, 2100, 3000, 1700], 2500);
// Sorted: 1200 1700 1900 2100 2300 2600 3000 4100, and Math.ceil(0.75 * 8) is 6
// { p75: 2600, goodShare: 0.625, passes: false }

What the tests check

  1. the 75th percentile of four samples is the third smallest
    expect(fieldSummary([1200, 1800, 2400, 3000], 2500)).toEqual({ p75: 2400, goodShare: 0.75, passes: true });
    
  2. sorts the samples as numbers, without reordering the array it was given
    const samples = [900, 10000, 1200, 300];
    expect(fieldSummary(samples, 2500)).toEqual({ p75: 1200, goodShare: 0.75, passes: true });
    expect(samples).toEqual([900, 10000, 1200, 300]);
    
  3. takes the nearest rank: the 8th of 10, the 4th of 5, and the only one of 1
    expect(fieldSummary([1000, 900, 800, 700, 600, 500, 400, 300, 200, 100], 2500).p75).toBe(800);
    expect(fieldSummary([500, 100, 400, 200, 300], 2500).p75).toBe(400);
    expect(fieldSummary([1234], 2500)).toEqual({ p75: 1234, goodShare: 1, passes: true });
    
  4. a sample right on the threshold is good
    expect(fieldSummary([2500, 1000, 2600, 2500], 2500)).toEqual({ p75: 2500, goodShare: 0.75, passes: true });
    
  5. one very slow visit doesn't move the 75th percentile
    const visits = [1000, 1100, 1200, 1300, 1400, 1500, 1600, 1700, 1800, 60000];
    expect(fieldSummary(visits, 2500)).toEqual({ p75: 1700, goodShare: 0.9, passes: true });
    
  6. passes only when at least three in four samples are good
    const sevenGood = [40, 56, 72, 88, 104, 120, 136, 240, 320, 480];
    expect(fieldSummary(sevenGood, 200)).toEqual({ p75: 240, goodShare: 0.7, passes: false });
    const eightGood = [40, 56, 72, 88, 104, 120, 136, 152, 240, 320];
    expect(fieldSummary(eightGood, 200)).toEqual({ p75: 152, goodShare: 0.8, passes: true });
    
  7. works on CLS scores as well as milliseconds
    expect(fieldSummary([0.3, 0, 0.05, 0.02], 0.1)).toEqual({ p75: 0.05, goodShare: 0.75, passes: true });
    expect(fieldSummary([0.3, 0.12, 0.05, 0.02], 0.1)).toEqual({ p75: 0.12, goodShare: 0.5, passes: false });
    
  8. no samples is null
    expect(fieldSummary([], 2500)).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).