JS Guide
HomeQuestionsTopicsCompaniesResources
BookmarksSearch

Built for developers preparing for JavaScript, React & TypeScript interviews.

ResourcesQuestionsSupport
HomeQuestionsSearchProgress
HomeTopicstestingPerformance Testing and Budgets
PrevNext
testing
advanced
15 min read

Performance Testing and Budgets

benchmarks
budgets
bundle-size
core-web-vitals
lighthouse
performance-testing
size-limit
vitest-bench

How to test application performance with benchmarks, Web Vitals monitoring, Lighthouse CI integration, and performance budgets that prevent regressions in bundle size and runtime metrics.

Key Points

1Benchmark Testing

Measure function execution times to compare implementations and detect performance regressions in critical code paths using vitest bench or benchmark.js.

2Core Web Vitals

LCP (loading speed < 2.5s), INP (interactivity < 200ms), and CLS (visual stability < 0.1) are Google's key metrics for real-world user experience performance.

3Performance Budgets

Set maximum thresholds for bundle size, request count, and timing metrics. Enforce in CI to prevent the gradual performance degradation that individual changes cause.

4Lighthouse CI

Automates Lighthouse audits in CI pipelines with configurable score thresholds and historical tracking to catch performance regressions before deployment.

What You'll Learn

  • Set up benchmark tests for critical functions using vitest bench or benchmark libraries
  • Explain Core Web Vitals and their target thresholds for good user experience
  • Configure performance budgets for bundle size and timing metrics in CI
  • Integrate Lighthouse CI into a deployment pipeline for automated performance auditing

Deep Dive

Performance testing ensures your application meets speed and efficiency requirements and catches regressions before they reach production. Unlike functional tests that verify correctness, performance tests verify that code runs fast enough, bundles stay small enough, and user experience metrics meet thresholds.

Types of Performance Testing

  1. Benchmark tests: Measure execution time of specific functions or operations. Useful for comparing algorithm implementations or detecting performance regressions in hot paths.
JavaScript
import { bench, describe } from 'vitest';
 
describe('array sorting', () => {
  bench('native sort', () => {
    [...largeArray].sort((a, b) => a - b);
  });
  bench('custom quicksort', () => {
    quickSort([...largeArray]);
  });
});

Vitest includes built-in benchmarking with vitest bench. For Jest projects, use libraries like benchmark.js or tinybench.

  1. Web Vitals testing: Core Web Vitals (LCP, INP, CLS) are Google's metrics for user experience. Testing them ensures your application meets real-world performance standards:
  • LCP (Largest Contentful Paint): How quickly the main content loads (target: < 2.5s)
  • INP (Interaction to Next Paint): How responsive the page is to user input (target: < 200ms)
  • CLS (Cumulative Layout Shift): How much the page layout shifts during loading (target: < 0.1)
  1. Load testing: Simulates concurrent users hitting your API or application to find breaking points. Tools like k6, Artillery, or autocannon stress-test server endpoints.

  2. Bundle size testing: Monitors JavaScript bundle sizes to prevent bloat. Bundlesize, Size Limit, or bundlephobia integration catches oversized dependencies.

Lighthouse CI

Lighthouse CI runs Google Lighthouse audits in your CI pipeline, scoring performance, accessibility, best practices, and SEO:

YAML
# .lighthouserc.json
{
  "ci": {
    "assert": {
      "assertions": {
        "categories:performance": ["error", { "minScore": 0.9 }],
        "first-contentful-paint": ["error", { "maxNumericValue": 2000 }]
      }
    }
  }
}

Lighthouse CI can upload results to a server for historical tracking and comparison between builds.

Performance Budgets

Performance budgets are thresholds that your application must not exceed:

  • Bundle size budget: Total JavaScript should be under a target (e.g., 200KB gzipped)
  • Image budget: Total image weight per page
  • Request count budget: Maximum number of HTTP requests
  • Timing budget: LCP under 2.5s, Time to Interactive under 3.8s

Enforce budgets in CI so builds fail when budgets are exceeded. This prevents the gradual performance degradation that happens when individual changes each add a small amount of weight.

Performance Budget Tools

  • size-limit: Check bundle size against a budget in CI
  • bundlewatch: Track and compare bundle sizes across PRs
  • lighthouse-ci: Automated Lighthouse checks with configurable assertions
  • Next.js built-in: experimental.outputFileTracingIncludes and bundle analyzer

React-Specific Performance Testing

  • Use React DevTools Profiler to record component render times
  • Test that React.memo and useMemo prevent unnecessary re-renders
  • Measure component mount/update times in benchmarks
  • Use why-did-you-render library to detect wasteful re-renders during development

CI Integration Strategy

  • Run bundle size checks on every PR (fast, catches size regressions)
  • Run Lighthouse CI on staging deployments (catches runtime performance issues)
  • Run load tests on a schedule (weekly) or before major releases
  • Track metrics over time to identify gradual degradation trends

Fun Fact

Google's Core Web Vitals replaced FID (First Input Delay) with INP (Interaction to Next Paint) in March 2024 because FID only measured the delay of the first interaction, while INP measures responsiveness throughout the entire page lifecycle, giving a much more accurate picture of user experience.

Learn These First

Testing Strategy for Large Applications

advanced

Integration Tests vs Unit Tests

intermediate

Continue Learning

End-to-End Testing

advanced

Visual Regression Testing

advanced

Practice What You Learned

How do you test application performance and prevent regressions?
senior
performance
Performance testing includes benchmarks for code execution time, Lighthouse CI for web vitals, and load testing with tools like k6. Set performance budgets, run tests in CI, and alert on regressions. Focus on critical user paths.
Previous
Mocking Functions, Modules, and APIs
Next
React Testing Library
PrevNext