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.
Types of Performance Tests:
Benchmark Tests:
Web Vitals Testing:
Load Testing:
Performance Budgets:
CI Integration:
// 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();