Performance Testing
Benchmark hot paths, detect regressions, and measure load with Vitest bench and k6.
What you'll learn
- The difference between micro-benchmarks and load tests
- Writing benchmarks with Vitest bench and Benchmark.js
- Detecting performance regressions in CI
- Load testing APIs with k6 and measuring throughput
Concept
Two Kinds of Performance Tests
Micro-benchmarks measure the speed of a small piece of code — a sort function, a parser, a reducer — so you can compare implementations and catch regressions. Load tests drive a running system with realistic or extreme traffic to find its limits: requests per second, latency percentiles, and the breaking point. Both matter; they answer different questions.
Vitest bench
Vitest ships a bench API for micro-benchmarks. bench("sort 1000 items", () => { sort(data) }) runs the function many times, warms up the JIT, and reports operations per second and average time. Run with vitest bench and compare results across runs. Keep benchmarks isolated (no I/O, no randomness) and vary the input size so you see the scaling curve, not just one point.
Regression Detection
A benchmark is only useful if you track its results. Store baseline numbers and fail CI when a benchmark regresses beyond a threshold (say, 10% slower). Beware of noise: CI machines are virtualized and variable, so use a tolerance band and prefer relative comparisons (before vs after a change) over absolute targets. Run benchmarks on a dedicated, quiet machine for the most stable numbers.
Load Testing with k6
k6 (and Artillery, wrk) drives a deployed API with scripted traffic and reports latency percentiles (p50, p95, p99) and throughput. Write a k6 script that ramps virtual users from 10 to 1000 over a minute, hits your endpoints, and asserts that p95 latency stays under 200ms. Run load tests against a staging environment, never production, and use them to find the bottleneck (database, connection pool, garbage collection) before users do.
In Practice
For micro-benchmarks, always compare two implementations side by side rather than measuring one in isolation — relative comparison cancels out machine noise. Vary the input size (10, 100, 1000, 10000) to see the scaling curve and catch accidental quadratic behavior. For load tests, start with a small ramp to find the shape of the performance curve, then design a sustained test that holds the system at its expected peak. Watch the p99 latency, not just the average — a low average with a high p99 means some users are waiting seconds. After a load test, correlate the bottlenecks with profiling data (CPU flames, database slow queries) so you fix the cause rather than throwing more hardware at the symptom. Check performance regressions in CI with a tolerance band, since virtualized CI machines are noisy.
Example
// sort.bench.ts — micro-benchmark with Vitest
import { bench, describe } from "vitest";
import { fastSort } from "./fastSort";
import { builtinSort } from "./builtinSort";
const data = Array.from({ length: 1000 }, (_, i) => Math.random() * i);
describe("sorting 1000 numbers", () => {
bench("fastSort", () => {
fastSort([...data]);
});
bench("builtinSort", () => {
builtinSort([...data]);
});
});
// Run: npx vitest bench
// Reports ops/sec and average time for each implementation.
bench runs each function many times and reports operations per second. Comparing two implementations side by side shows which is faster and by how much.
Try it
- HTTP Request Tester
Measure the latency of endpoints before load testing them.
- Crontab Generator
Schedule recurring performance runs against staging.
Common mistakes
The mistake
Benchmarking with cold data the JIT has never seen
The fix
Let the benchmark warm up (Vitest does this automatically) and run enough iterations that the JIT optimizes. One-shot timing is meaningless.
The mistake
Load testing production
The fix
Always load test a staging or isolated environment. A load test against production can overload real users and trigger outages.
Look up unfamiliar terms
A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.
Build real projects
Apply what you learned by building guided projects with starter code and solutions.
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.