Skip to the code

Catch a p75 regression

Monitoring compares this week's field data with last week's, and CI compares a pull request's lab runs with the main branch's. Either way, one slow visit shouldn't page anyone, and three samples shouldn't decide anything. Write detectRegression(baseline, candidate, { minSamples, threshold }). baseline and candidate are arrays of samples of one metric where lower is better (LCP or INP in ms, or CLS scores), minSamples is at least 1, and threshold is a percentage. Return { baseline, candidate, change, enough, regressed }.

baseline and candidate in the result are each array's 75th percentile by the nearest-rank rule, as Core Web Vitals are judged: sort a copy from smallest to largest and take the value at position Math.ceil(0.75 * n), counting from 1, or null when there are no samples. change is how far the candidate's p75 moved, as a percentage of the baseline's, (candidate - baseline) / baseline * 100 rounded to one decimal with Math.round(change * 10) / 10. It's null when either p75 is null or the baseline's is 0.

enough is true when both arrays have at least minSamples samples. regressed is true only when there are enough samples and the candidate's p75 is worse than the baseline's by more than threshold percent. Decide that without dividing, so a change of exactly threshold percent can't tip over on floating-point error: it's a regression when (candidateP75 - baselineP75) * 100 > threshold * baselineP75. With a baseline p75 of 0, any candidate p75 above 0 is a regression.

detectRegression(
  [2100, 2300, 1900, 2500, 2200, 2400, 2000, 2600], // the menu page's LCP on main
  [2300, 2500, 2100, 2900, 2400, 2800, 2200, 3000], // with the new hero photo of oolong
  { minSamples: 8, threshold: 10 },
);
// The 6th of 8: 2400 before and 2800 after, 400 ms worse, which is 16.7%
// { baseline: 2400, candidate: 2800, change: 16.7, enough: true, regressed: true }

What the tests check

  1. flags a p75 that got worse by more than the threshold
    const baseline = [2100, 2300, 1900, 2500, 2200, 2400, 2000, 2600];
    const candidate = [2300, 2500, 2100, 2900, 2400, 2800, 2200, 3000];
    expect(detectRegression(baseline, candidate, { minSamples: 8, threshold: 10 })).toEqual({ baseline: 2400, candidate: 2800, change: 16.7, enough: true, regressed: true });
    
  2. a p75 that improved, or got worse by less than the threshold, isn't a regression
    const baseline = [2100, 2300, 1900, 2500, 2200, 2400, 2000, 2600];
    const slower = [2000, 2100, 2200, 2300, 2400, 2500, 2600, 2700];
    expect(detectRegression(baseline, slower, { minSamples: 8, threshold: 10 })).toEqual({ baseline: 2400, candidate: 2500, change: 4.2, enough: true, regressed: false });
    const faster = [1800, 1900, 2000, 2050, 2100, 2200, 2300, 2400];
    expect(detectRegression(baseline, faster, { minSamples: 8, threshold: 10 })).toEqual({ baseline: 2400, candidate: 2200, change: -8.3, enough: true, regressed: false });
    
  3. exactly the threshold isn't a regression, and a little past it is
    const baseline = [1000, 1500, 2000, 2500];
    expect(detectRegression(baseline, [1100, 1600, 2200, 2700], { minSamples: 4, threshold: 10 })).toEqual({ baseline: 2000, candidate: 2200, change: 10, enough: true, regressed: false });
    expect(detectRegression(baseline, [1100, 1600, 2202, 2700], { minSamples: 4, threshold: 10 })).toEqual({ baseline: 2000, candidate: 2202, change: 10.1, enough: true, regressed: true });
    const inp = [80, 96, 104, 120, 136, 144, 160, 176];
    const worse = [88, 100, 112, 128, 144, 158, 168, 184];
    expect(detectRegression(inp, worse, { minSamples: 8, threshold: 10 })).toEqual({ baseline: 144, candidate: 158, change: 9.7, enough: true, regressed: false });
    
  4. too few samples is never a regression, however much worse
    const baseline = [2100, 2300, 1900, 2500, 2200, 2400, 2000, 2600];
    expect(detectRegression(baseline, [5000, 5200, 5400], { minSamples: 5, threshold: 10 })).toEqual({ baseline: 2400, candidate: 5400, change: 125, enough: false, regressed: false });
    expect(detectRegression([2000, 2100], baseline, { minSamples: 5, threshold: 10 })).toEqual({ baseline: 2100, candidate: 2400, change: 14.3, enough: false, regressed: false });
    
  5. one very slow sample doesn't move the p75
    const baseline = [900, 1000, 1100, 1200, 1300, 1400, 1500, 1600];
    const candidate = [1000, 1100, 1200, 1300, 1400, 1450, 1500, 60000];
    expect(detectRegression(baseline, candidate, { minSamples: 8, threshold: 5 })).toEqual({ baseline: 1400, candidate: 1450, change: 3.6, enough: true, regressed: false });
    
  6. sorts copies of the samples as numbers
    const baseline = [900, 10000, 1200, 300];
    const candidate = [950, 1300, 10000, 400];
    expect(detectRegression(baseline, candidate, { minSamples: 4, threshold: 5 })).toEqual({ baseline: 1200, candidate: 1300, change: 8.3, enough: true, regressed: true });
    expect(baseline).toEqual([900, 10000, 1200, 300]);
    expect(candidate).toEqual([950, 1300, 10000, 400]);
    
  7. a CLS baseline of 0 has no change, and any shift above it is a regression
    const baseline = [0, 0, 0, 0.02];
    expect(detectRegression(baseline, [0, 0, 0.05, 0.08], { minSamples: 4, threshold: 10 })).toEqual({ baseline: 0, candidate: 0.05, change: null, enough: true, regressed: true });
    expect(detectRegression(baseline, [0, 0, 0, 0.3], { minSamples: 4, threshold: 10 })).toEqual({ baseline: 0, candidate: 0, change: null, enough: true, regressed: false });
    
  8. no samples is a p75 of null
    expect(detectRegression([], [1200], { minSamples: 1, threshold: 10 })).toEqual({ baseline: null, candidate: 1200, change: null, enough: false, regressed: false });
    expect(detectRegression([], [], { minSamples: 1, threshold: 10 })).toEqual({ baseline: null, candidate: null, change: null, enough: false, regressed: false });
    

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