Skip to the code

Score a metric like Lighthouse

Lighthouse doesn't score a metric against a single threshold. Each metric has two control points: p10, the value that scores 0.9, and median, the value that scores 0.5 (2500 and 4000 ms for LCP on mobile, 200 and 600 ms for TBT). Around them the score follows a log-normal curve. Write metricScore({ p10, median }, value) that returns the score Lighthouse 13.6.0 gives value: a number from 0 to 1 with two decimals.

Throw an Error unless median and p10 are both above 0 and p10 is less than median. A value of 0 or less scores 1. Otherwise compute z = Math.log(value / median) * 0.9061938024368232 / -Math.log(p10 / median) (the constant is the inverse of erfc at 1/5), and the raw score is (1 - erf(z)) / 2: the share of the curve above value.

JavaScript has no erf, so Lighthouse uses Abramowitz and Stegun's formula 7.1.26. For x of 0 or more, t = 1 / (1 + 0.3275911 * x) and erf(x) = 1 - t * (a1 + t * (a2 + t * (a3 + t * (a4 + t * a5)))) * Math.exp(-x * x), with a1 = 0.254829592, a2 = -0.284496736, a3 = 1.421413741, a4 = -1.453152027, and a5 = 1.061405429. For a negative x, erf(x) is -erf(-x).

The formula is off by up to about 1.5e-7, so Lighthouse keeps each raw score in the band its value belongs to: a value at or below p10 scores from 0.9 to 1; above p10 and up to median, from 0.5 to just under 0.9 (0.9 - Number.EPSILON / 2, the largest double below 0.9); above median, from 0 to just under 0.5 (0.5 - Number.EPSILON / 4). A score above 0.9 then gets a small boost, score + 0.05 * (score - 0.9), so more fast pages reach 1. Last, round down to two decimals: Math.floor(score * 100) / 100.

metricScore({ p10: 2500, median: 4000 }, 3000); // 0.78: an LCP of 3 s on mobile
metricScore({ p10: 2500, median: 4000 }, 2500); // 0.9: right on p10
metricScore({ p10: 0.1, median: 0.25 }, 0.05); // 0.99: a CLS of 0.05, with the boost

What the tests check

  1. p10 scores 0.9 and the median scores 0.5
    expect(metricScore({ p10: 2500, median: 4000 }, 2500)).toBe(0.9);
    expect(metricScore({ p10: 2500, median: 4000 }, 4000)).toBe(0.5);
    expect(metricScore({ p10: 200, median: 600 }, 200)).toBe(0.9);
    expect(metricScore({ p10: 200, median: 600 }, 600)).toBe(0.5);
    expect(metricScore({ p10: 3387, median: 5800 }, 3387)).toBe(0.9);
    
  2. scores along the curve
    const lcp = { p10: 2500, median: 4000 };
    expect(metricScore(lcp, 3000)).toBe(0.78);
    expect(metricScore(lcp, 3200)).toBe(0.72);
    expect(metricScore(lcp, 5000)).toBe(0.27);
    expect(metricScore(lcp, 6000)).toBe(0.13);
    expect(metricScore(lcp, 8000)).toBe(0.02);
    const tbt = { p10: 200, median: 600 };
    expect(metricScore(tbt, 300)).toBe(0.79);
    expect(metricScore(tbt, 1000)).toBe(0.27);
    expect(metricScore({ p10: 0.1, median: 0.25 }, 0.3)).toBe(0.39);
    
  3. a value just past a control point is in the next band
    expect(metricScore({ p10: 2500, median: 4000 }, 2500.0001)).toBe(0.89);
    expect(metricScore({ p10: 2500, median: 4000 }, 4000.0001)).toBe(0.49);
    expect(metricScore({ p10: 0.1, median: 0.25 }, 0.1000001)).toBe(0.89);
    expect(metricScore({ p10: 2500, median: 4000 }, 2499.9999)).toBe(0.9);
    
  4. scores above 0.9 get the boost
    expect(metricScore({ p10: 0.1, median: 0.25 }, 0.05)).toBe(0.99);
    expect(metricScore({ p10: 2500, median: 4000 }, 1500)).toBe(1);
    expect(metricScore({ p10: 200, median: 600 }, 50)).toBe(1);
    expect(metricScore({ p10: 2500, median: 4000 }, 1800)).toBe(0.98);
    
  5. rounds down, never to the nearest
    expect(metricScore({ p10: 200, median: 600 }, 150)).toBe(0.94);
    expect(metricScore({ p10: 2500, median: 4000 }, 2400)).toBe(0.91);
    expect(metricScore({ p10: 1200, median: 2400 }, 3000)).toBe(0.33);
    
  6. 0 or less scores 1, and a very slow value scores 0
    expect(metricScore({ p10: 2500, median: 4000 }, 0)).toBe(1);
    expect(metricScore({ p10: 2500, median: 4000 }, -5)).toBe(1);
    expect(metricScore({ p10: 0.1, median: 0.25 }, 0)).toBe(1);
    expect(metricScore({ p10: 2500, median: 4000 }, 20000)).toBe(0);
    
  7. works with desktop's control points too
    const lcp = { p10: 1200, median: 2400 };
    expect(metricScore(lcp, 1000)).toBe(0.94);
    expect(metricScore(lcp, 1500)).toBe(0.8);
    expect(metricScore(lcp, 1800)).toBe(0.7);
    expect(metricScore({ p10: 934, median: 1600 }, 934)).toBe(0.9);
    
  8. control points that don't make a curve are an error
    expect(() => metricScore({ p10: 2500, median: 0 }, 3000)).toThrow(Error);
    expect(() => metricScore({ p10: 0, median: 4000 }, 3000)).toThrow(Error);
    expect(() => metricScore({ p10: 4000, median: 4000 }, 3000)).toThrow(Error);
    expect(() => metricScore({ p10: 5000, median: 4000 }, 3000)).toThrow(Error);
    

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