JS Guide
HomeQuestionsSearchResources
Search

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

ResourcesQuestionsSupport
HomeQuestionsSearchProgress
HomeQuestionstesting
PrevNext
testing
senior
performance

How do you test application performance and prevent regressions?

performance-testing
lighthouse
benchmarks
budgets
Quick Answer

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.

Detailed Explanation

Types of Performance Tests:

  1. Benchmark Tests:

    • Function execution time
    • Memory usage
    • CPU profiling
  2. Web Vitals Testing:

    • LCP, FID, CLS
    • Lighthouse CI
    • Real user monitoring
  3. Load Testing:

    • Concurrent users
    • Response times under load
    • Breaking points

Performance Budgets:

  • Bundle size limits
  • Time to interactive
  • API response times

CI Integration:

  • Run on every PR
  • Compare against baseline
  • Block merges on regression

Code Examples

Benchmark testing
// benchmark.test.js
describe('Performance benchmarks', () => {
  test('array sort performance', () => {
    const largeArray = Array.from({ length: 10000 }, () => Math.random());
    
    const start = performance.now();
    largeArray.sort((a, b) => a - b);
    const duration = performance.now() - start;
    
    expect(duration).toBeLessThan(50); // Should complete in 50ms
  });

  test('component render time', async () => {
    const start = performance.now();
    
    const { container } = render(<LargeList items={Array(1000).fill({})} />);
    
    const duration = performance.now() - start;
    expect(duration).toBeLessThan(100);
  });
});

// Using benchmark.js for detailed profiling
import Benchmark from 'benchmark';

const suite = new Benchmark.Suite();

suite
  .add('RegExp#test', () => {
    /o/.test('Hello World!');
  })
  .add('String#includes', () => {
    'Hello World!'.includes('o');
  })
  .on('complete', function() {
    console.log('Fastest is ' + this.filter('fastest').map('name'));
  })
  .run();

Resources

Lighthouse CI

docs

Web Vitals

article

Related Questions

How do you design a testing strategy for a large application?

senior
strategy

What are E2E tests and when should you use Cypress vs Playwright?

senior
e2e
Previous
How do you design a testing strategy for a large application?
Next
What is visual regression testing and how do you implement it?