Skip to the code

Lighthouse's Performance score

Lighthouse's Performance score is a weighted average of five metric scores, each from 0 to 1. Write performanceScore(scores). scores has each metric's score by its acronym: FCP, SI (Speed Index), LCP, TBT, and CLS, and sometimes INP. A score is null when that metric's audit failed. Return the number the report's gauge shows: a whole number from 0 to 100, or null.

Lighthouse 13.6.0's default config weighs FCP 10, SI 10, LCP 25, TBT 30, and CLS 25. INP weighs 0, so it never counts, even when it's null. Multiply each score by its weight, add them up, and divide by the total weight, 100. Lighthouse rounds that to two decimals with Math.round(mean * 100) / 100, and the gauge shows Math.round(score * 100). If any metric that has a weight is null, return null.

performanceScore({ FCP: 0.92, SI: 0.85, LCP: 0.61, TBT: 0.4, CLS: 1, INP: null });
// 9.2 + 8.5 + 15.25 + 12 + 25 = 69.95, so a mean of 0.6995, a score of 0.7, and 70 on the gauge

What the tests check

  1. every metric at 1 is 100, and every metric at 0 is 0
    expect(performanceScore({ FCP: 1, SI: 1, LCP: 1, TBT: 1, CLS: 1 })).toBe(100);
    expect(performanceScore({ FCP: 0, SI: 0, LCP: 0, TBT: 0, CLS: 0 })).toBe(0);
    
  2. TBT weighs the most, then LCP and CLS, then FCP and SI
    const perfect = { FCP: 1, SI: 1, LCP: 1, TBT: 1, CLS: 1 };
    expect(performanceScore({ ...perfect, TBT: 0 })).toBe(70);
    expect(performanceScore({ ...perfect, LCP: 0 })).toBe(75);
    expect(performanceScore({ ...perfect, CLS: 0 })).toBe(75);
    expect(performanceScore({ ...perfect, FCP: 0 })).toBe(90);
    expect(performanceScore({ ...perfect, SI: 0 })).toBe(90);
    
  3. a weighted average of the five
    expect(performanceScore({ FCP: 0.92, SI: 0.85, LCP: 0.61, TBT: 0.4, CLS: 1 })).toBe(70);
    expect(performanceScore({ FCP: 0.9, SI: 0.8, LCP: 0.6, TBT: 0.5, CLS: 1 })).toBe(72);
    expect(performanceScore({ FCP: 0.81, SI: 0.77, LCP: 0.66, TBT: 0.93, CLS: 0.88 })).toBe(82);
    
  4. INP doesn't count, even when it's null
    const scores = { FCP: 0.9, SI: 0.8, LCP: 0.6, TBT: 0.5, CLS: 1 };
    expect(performanceScore({ ...scores, INP: 0 })).toBe(72);
    expect(performanceScore({ ...scores, INP: null })).toBe(72);
    expect(performanceScore({ ...scores, INP: 1 })).toBe(72);
    
  5. a failed metric makes the whole score null
    expect(performanceScore({ FCP: 0.9, SI: 0.8, LCP: 0.6, TBT: null, CLS: 1 })).toBe(null);
    expect(performanceScore({ FCP: 1, SI: 1, LCP: null, TBT: 1, CLS: 1, INP: 1 })).toBe(null);
    
  6. rounds to the nearest point, and half a point up
    expect(performanceScore({ FCP: 0.98, SI: 0.97, LCP: 0.78, TBT: 0.79, CLS: 0.99 })).toBe(87);
    expect(performanceScore({ FCP: 0.83, SI: 0.83, LCP: 0.9, TBT: 0.9, CLS: 0.9 })).toBe(89);
    expect(performanceScore({ FCP: 0.95, SI: 1, LCP: 1, TBT: 1, CLS: 1 })).toBe(100);
    expect(performanceScore({ FCP: 1, SI: 0.45, LCP: 1, TBT: 1, CLS: 1 })).toBe(95);
    

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