Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 43

Implementing Performance Budgets in CI/CD

Recommended resources

In our previous lesson, we established the critical difference between synthetic and real user monitoring, concluding that a robust observability strategy needs both. You learned how to get data from the lab (synthetic) to proactively catch issues and from the field (RUM) to understand true user impact. Now, we'll take the next logical step: transforming those measurements from passive data points into active, automated guards for your application's health.

This lesson focuses on a concept that is a hallmark of a senior engineer's mindset: performance budgets. You will learn how to define strict, quantifiable limits for key performance metrics and, most importantly, how to enforce these budgets within a CI/CD pipeline. This is how you shift an organization from reactively fixing performance fires to proactively preventing them from ever starting.

1. From Aspirational Goal to System Constraint

In many organizations, performance is treated as an aspirational goal. Teams work hard to make the site fast, but over time, as new features and dependencies are added, performance inevitably degrades. This is often called the "boiling frog problem"—a slow, incremental decline that goes unnoticed until the application is painfully slow, triggering a costly, all-hands-on-deck "performance sprint."

A performance budget stops this cycle. It's not a target; it's a non-negotiable constraint baked into your development process.

Frontend System Design: Performance Monitoring & Observability

This article provides an excellent introduction to performance budgets and perfectly illustrates the "boiling frog problem."

Read the section titled Performance Budgets. Pay close attention to the example scenario that contrasts development with and without a budget. This crystallizes the value proposition of automated enforcement.

As the article highlights, the goal is to make performance a gatekeeper. A pull request that violates the budget should fail the build, forcing a conversation about trade-offs before the regression is merged. This transforms performance from an afterthought into a fundamental design constraint.


2. Defining Your Budgets: A Multi-Layered Approach

A common mistake is to think of a performance budget as a single number, like "the JavaScript bundle must be under 200KB." While that's a good start, a mature strategy defines budgets across multiple layers of the system, each targeting a different aspect of the user experience.

This diagram shows how performance budgets can be applied as constraints across different layers of your application architecture, from dependencies and network assets to code execution and final rendering.

We can categorize budgets into three main types.

Frontend System Design: Performance Monitoring & Observability

This article provides a clear breakdown of the different types of budgets and a useful template.

Focus on the tables under the headings Types of Performance Budgets and Recommended Budget Template.

Let's summarize the key budget categories from the reading:

  • Quantity-based Budgets: These are the simplest to measure and enforce. They set limits on the "stuff" you send over the wire.

    • Examples: Max JS bundle size (e.g., < 170KB), max total image weight, max number of third-party scripts.
    • Pros: Easy to understand, deterministic, and excellent for catching dependency bloat.
    • Cons: Don't directly measure user experience. A small bundle can still be slow if its execution cost is high.
  • Timing-based Budgets: These are tied directly to user-centric metrics, especially the Core Web Vitals.

    • Examples: Largest Contentful Paint (LCP) < 2.5s, Interaction to Next Paint (INP) < 200ms, Cumulative Layout Shift (CLS) < 0.1.
    • Pros: Directly measure perceived performance and align with Google's ranking signals.
    • Cons: Can have variability (especially in CI environments), making it harder to set precise failure thresholds.
  • Score-based Budgets: These use a composite score from an auditing tool like Lighthouse.

    • Example: Lighthouse Performance Score > 90.
    • Pros: A single, easy-to-track number that summarizes overall performance health.
    • Cons: An aggregate score can mask a serious regression in a single underlying metric.

A senior engineer designs a strategy that combines all three. You might set a hard limit on bundle size (quantity), an assertion on the LCP time (timing), and a goal for the overall Lighthouse score (score-based).


3. Enforcing Budgets in Your CI/CD Pipeline

Defining budgets is easy. Enforcing them is what separates high-performing teams from the rest. The key is automation within your CI/CD pipeline, creating a feedback loop that is immediate and unavoidable.

Let's look at the primary tools for this job. The article you just read provides a great overview table.

Frontend Performance Budgets: How to Keep Apps Fast as Teams Scale - Landskill

This article from Landskill offers a strategic perspective on how budgets function as enforceable constraints within delivery systems.

Read the section Enforcing Budgets in CI/CD. It frames this not just as a tool, but as a "continuous validation mechanism."

Now, let's get practical and look at the two most common enforcement patterns.

Pattern 1: Bundle Size Budgeting with size-limit

For quantity-based budgets, especially JavaScript bundle size, a tool like size-limit is invaluable. It runs in your CI pipeline, calculates the real cost of your JavaScript to a user (size + initial execution time), and fails the build if it exceeds a configured limit. The feedback is often posted directly as a comment on the pull request.

An example of a GitHub Actions bot comment from `size-limit`. It clearly shows the bundle size, its impact on loading time, and flags a significant regression, providing immediate feedback to the developer on their pull request.

This kind of immediate, unambiguous feedback is incredibly powerful. The developer doesn't need to be a performance expert to understand that their change added 46% to the bundle size and that this is unacceptable.

Pattern 2: Metric Budgeting with Lighthouse CI

For timing- and score-based budgets, Lighthouse CI is the industry standard. It allows you to run Lighthouse audits automatically on every pull request and set assertions that fail the build if your budgets are violated.

The following videos walk you through setting this up. We will focus on the key configuration steps, not the boilerplate code.

CI #8 - Automated Lighthouse Benchmarks

This video from the "Swashbuckling with Code" channel provides a clear, step-by-step guide to setting up Lighthouse CI from scratch.

First, watch the segment on configuring the lighthouserc.js file with assertions, using the lighthouse:recommended preset. This is where you first tell Lighthouse what to check for assertion presets. Next, see how the command is added to the CI workflow file, ensuring it runs after the build step in the workflow. Finally, watch the crucial section on customizing assertions, where the presenter shows how to set minimum scores for categories like performance and SEO customizing scores. This is how you define your actual budgets.

The key file is lighthouserc.js (or .json), where you define your assert block. For example:

// lighthouserc.js
module.exports = {
  ci: {
    assert: {
      preset: 'lighthouse:recommended',
      assertions: {
        'categories:performance': ['error', { minScore: 0.9 }], // Performance score must be >= 90
        'categories:accessibility': ['error', { minScore: 0.95 }], // Accessibility score must be >= 95
        'core/largest-contentful-paint': ['warn', { maxNumericValue: 2500 }], // LCP should be < 2.5s (warn only)
      },
    },
    // ... other configurations
  },
};

This configuration tells the CI job to fail (error) if the performance score drops below 90, but only issue a warn if LCP exceeds 2500ms. This allows for a tiered approach where you can be strict about some metrics while being more lenient on others that might have more variability.

To see how the CI results are integrated back into the developer workflow, let's look at another short demonstration.

Lighthouse CI: Adding a Google Lighthouse Audit to Your CI CD Process

This video from Guiding Digital focuses on the GitHub-specific integration of Lighthouse CI.

Watch the section that explains how to display the Lighthouse results as a "status check" directly on the pull request as status checks. This step is crucial for developer experience; it makes the budget feedback impossible to ignore.


4. Senior-Level Strategy: From Enforcement to Governance

A mid-level engineer can set up Lighthouse CI. A senior engineer designs the strategy for its rollout and governance across teams. Simply turning on strict budgets for a large, existing application will cause chaos.

A pragmatic, senior-led approach looks like this:

  1. Audit and Baseline: First, run Lighthouse CI on your main branch for a week to establish a stable baseline. Don't set any failing assertions yet. You need to understand your current performance before you can budget for it.
  2. Establish "Warning" Budgets: Based on your baseline, set initial budgets as warn levels in your Lighthouse CI configuration. This surfaces regressions in pull requests without blocking developers. It starts building awareness and provides data on how volatile your metrics are.
  3. Introduce a Single Failing Budget: Pick one, highly important, and relatively stable metric to enforce. Bundle size is a perfect candidate. Configure size-limit to fail builds if the main bundle grows. This is your first hard line in the sand.
  4. Socialize and Tighten: Communicate the process to the team. Explain why these budgets are being introduced. As the team gets used to the bundle size budget, gradually tighten the Lighthouse budgets, moving them from warn to error, one metric at a time. Start with the most critical ones, like LCP or the overall performance score.
  5. Connect to RUM: Your CI budgets are based on lab data. Periodically review your field data from RUM to ensure your lab budgets correlate with a good real-world user experience. If your p75 LCP in the field is 4s, a lab budget of 2.5s might be a good target to drive improvements.

This incremental approach turns performance budgeting from a disruptive mandate into a collaborative process of raising quality.


Conclusion

In this lesson, we moved from monitoring performance to actively governing it. You learned that performance budgets are not just targets, but enforceable system constraints that prevent the slow, insidious decay of user experience. We explored how to define multi-layered budgets and, most critically, how to automate their enforcement within a CI/CD pipeline using industry-standard tools like size-limit and Lighthouse CI.

Key Takeaways:

  • Budgets Prevent Decay: Performance budgets solve the "boiling frog problem" by making performance regressions explicit and actionable at the moment they are introduced.
  • A Layered Approach is Key: Effective budgets combine quantity-based (bundle size), timing-based (Core Web Vitals), and score-based (Lighthouse score) metrics.
  • Automation is Everything: A budget without automated enforcement in CI/CD is merely a suggestion. Tools like size-limit and Lighthouse CI turn budgets into powerful, unavoidable quality gates.
  • Rollout is Strategic: Implementing budgets requires a phased approach: baseline, warn, then incrementally enforce. This builds team buy-in and avoids disruption.

In our next lesson, we will shift our focus from performance to stability. We'll design a client-side error reporting architecture, exploring how to capture, aggregate, and triage frontend errors at scale, ensuring you can find and fix bugs before they significantly impact your users.