Skip to the code

Check a build against its budget

A performance budget is a set of limits the page must stay under, so a change that goes over fails the build instead of reaching users. Write checkBudget(resources, budget). resources are the page's requests, each { url, type, transferSize } with the transfer size in bytes. budget is one budget in the budget.json format: Lighthouse read it until version 12 removed budgets, and Lighthouse CI still reads it (its budgetsFile option), checking each line against the resource summary that Lighthouse 13.6.0 still reports. Return every line of the budget that the page goes over.

The budget has two optional lists, resourceSizes and resourceCounts, of { resourceType, budget }. A size budget is in KiB, so multiply it by 1024 for bytes; a count budget is a number of requests. resourceType names one line of the summary: document, script, stylesheet, image, media, font, other, total (every request), or third-party (every request that isn't first party). A resource whose type isn't one of the first six counts as other, as Lighthouse files fetches and everything else it has no line for. A line is over only when it's above its limit, so 150 KiB of script passes a budget of 150. A line with no requests is 0, and a timings list is ignored here.

First party comes from budget.options.firstPartyHostnames, a list of hostnames. One that starts with *. matches every hostname that ends with the rest, so *.leafandkettle.com matches leafandkettle.com and cdn.leafandkettle.com; any other matches only itself. A budget with a third-party line always has the list here (without one, Lighthouse used the page's root domain).

Return an array of { resourceType, kind, limit, actual, over }. kind is "size", with limit, actual, and over in bytes, or "count", with them in requests. List the size lines first and then the count lines, each in the budget's order, and return [] when the page is within budget.

checkBudget(
  [
    { url: "https://leafandkettle.com/menu", type: "document", transferSize: 18000 },
    { url: "https://leafandkettle.com/js/menu.js", type: "script", transferSize: 140000 },
    { url: "https://cdn.leafandkettle.com/img/oolong.avif", type: "image", transferSize: 60000 },
    { url: "https://widgets.reviewly.example/embed.js", type: "script", transferSize: 32000 },
  ],
  {
    options: { firstPartyHostnames: ["*.leafandkettle.com"] },
    resourceSizes: [{ resourceType: "script", budget: 150 }, { resourceType: "total", budget: 300 }],
    resourceCounts: [{ resourceType: "third-party", budget: 0 }],
  },
);
// [
//   { resourceType: "script", kind: "size", limit: 153600, actual: 172000, over: 18400 },
//   { resourceType: "third-party", kind: "count", limit: 0, actual: 1, over: 1 },
// ]

What the tests check

  1. measures size budgets in KiB of 1024 bytes
    const resources = [
      { url: "https://leafandkettle.com/js/app.js", type: "script", transferSize: 120000 },
      { url: "https://leafandkettle.com/js/menu.js", type: "script", transferSize: 40000 },
      { url: "https://leafandkettle.com/css/site.css", type: "stylesheet", transferSize: 20000 },
    ];
    expect(checkBudget(resources, { resourceSizes: [{ resourceType: "script", budget: 150 }] })).toEqual([
      { resourceType: "script", kind: "size", limit: 153600, actual: 160000, over: 6400 },
    ]);
    expect(checkBudget(resources, { resourceSizes: [{ resourceType: "script", budget: 157 }, { resourceType: "stylesheet", budget: 20 }] })).toEqual([]);
    
  2. a line right on its limit is within budget
    const resources = [
      { url: "https://leafandkettle.com/js/app.js", type: "script", transferSize: 102400 },
      { url: "https://leafandkettle.com/img/green.avif", type: "image", transferSize: 30000 },
      { url: "https://leafandkettle.com/img/mint.avif", type: "image", transferSize: 30000 },
    ];
    expect(checkBudget(resources, { resourceSizes: [{ resourceType: "script", budget: 100 }], resourceCounts: [{ resourceType: "image", budget: 2 }] })).toEqual([]);
    expect(checkBudget(resources, { resourceSizes: [{ resourceType: "script", budget: 99 }], resourceCounts: [{ resourceType: "image", budget: 1 }] })).toEqual([
      { resourceType: "script", kind: "size", limit: 101376, actual: 102400, over: 1024 },
      { resourceType: "image", kind: "count", limit: 1, actual: 2, over: 1 },
    ]);
    
  3. counts requests per type, and total counts every request
    const resources = [
      { url: "https://leafandkettle.com/menu", type: "document", transferSize: 18000 },
      { url: "https://leafandkettle.com/js/app.js", type: "script", transferSize: 90000 },
      { url: "https://leafandkettle.com/js/menu.js", type: "script", transferSize: 30000 },
      { url: "https://leafandkettle.com/js/cart.js", type: "script", transferSize: 12000 },
      { url: "https://leafandkettle.com/css/site.css", type: "stylesheet", transferSize: 16000 },
    ];
    const counts = [{ resourceType: "script", budget: 2 }, { resourceType: "total", budget: 4 }, { resourceType: "image", budget: 10 }];
    expect(checkBudget(resources, { resourceCounts: counts })).toEqual([
      { resourceType: "script", kind: "count", limit: 2, actual: 3, over: 1 },
      { resourceType: "total", kind: "count", limit: 4, actual: 5, over: 1 },
    ]);
    expect(checkBudget(resources, { resourceSizes: [{ resourceType: "total", budget: 160 }] })).toEqual([
      { resourceType: "total", kind: "size", limit: 163840, actual: 166000, over: 2160 },
    ]);
    
  4. a type without a line of its own counts as other
    const resources = [
      { url: "https://leafandkettle.com/api/menu", type: "fetch", transferSize: 6000 },
      { url: "https://leafandkettle.com/manifest.json", type: "manifest", transferSize: 1000 },
      { url: "https://leafandkettle.com/data/teas.json", type: "other", transferSize: 4000 },
      { url: "https://leafandkettle.com/js/app.js", type: "script", transferSize: 50000 },
    ];
    const budget = { resourceSizes: [{ resourceType: "other", budget: 10 }], resourceCounts: [{ resourceType: "other", budget: 2 }] };
    expect(checkBudget(resources, budget)).toEqual([
      { resourceType: "other", kind: "size", limit: 10240, actual: 11000, over: 760 },
      { resourceType: "other", kind: "count", limit: 2, actual: 3, over: 1 },
    ]);
    
  5. third party is every request that no first-party hostname matches
    const resources = [
      { url: "https://leafandkettle.com/menu", type: "document", transferSize: 18000 },
      { url: "https://cdn.leafandkettle.com/img/oolong.avif", type: "image", transferSize: 40000 },
      { url: "https://assets.teacdn.example/fonts/lora.woff2", type: "font", transferSize: 24000 },
      { url: "https://img.teacdn.example/banner.avif", type: "image", transferSize: 30000 },
      { url: "https://widgets.reviewly.example/embed.js", type: "script", transferSize: 52000 },
    ];
    const budget = {
      options: { firstPartyHostnames: ["*.leafandkettle.com", "assets.teacdn.example"] },
      resourceSizes: [{ resourceType: "third-party", budget: 50 }],
      resourceCounts: [{ resourceType: "third-party", budget: 1 }],
    };
    expect(checkBudget(resources, budget)).toEqual([
      { resourceType: "third-party", kind: "size", limit: 51200, actual: 82000, over: 30800 },
      { resourceType: "third-party", kind: "count", limit: 1, actual: 2, over: 1 },
    ]);
    
  6. lists size lines, then count lines, each in the budget's order
    const resources = [
      { url: "https://leafandkettle.com/menu", type: "document", transferSize: 30000 },
      { url: "https://leafandkettle.com/js/app.js", type: "script", transferSize: 80000 },
      { url: "https://leafandkettle.com/img/green.avif", type: "image", transferSize: 50000 },
      { url: "https://leafandkettle.com/img/mint.avif", type: "image", transferSize: 50000 },
    ];
    const budget = {
      resourceCounts: [{ resourceType: "image", budget: 1 }, { resourceType: "total", budget: 3 }],
      resourceSizes: [{ resourceType: "total", budget: 200 }, { resourceType: "image", budget: 90 }, { resourceType: "document", budget: 20 }],
    };
    expect(checkBudget(resources, budget)).toEqual([
      { resourceType: "total", kind: "size", limit: 204800, actual: 210000, over: 5200 },
      { resourceType: "image", kind: "size", limit: 92160, actual: 100000, over: 7840 },
      { resourceType: "document", kind: "size", limit: 20480, actual: 30000, over: 9520 },
      { resourceType: "image", kind: "count", limit: 1, actual: 2, over: 1 },
      { resourceType: "total", kind: "count", limit: 3, actual: 4, over: 1 },
    ]);
    
  7. a budget with no lines, or a page with no requests, is within budget
    const resources = [{ url: "https://leafandkettle.com/menu", type: "document", transferSize: 18000 }];
    expect(checkBudget(resources, {})).toEqual([]);
    expect(checkBudget(resources, { timings: [{ metric: "interactive", budget: 1 }] })).toEqual([]);
    expect(checkBudget([], { resourceSizes: [{ resourceType: "total", budget: 0 }], resourceCounts: [{ resourceType: "font", budget: 0 }] })).toEqual([]);
    

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