From harness-claude
Measures and asserts code performance using Vitest bench, timing budgets, memory profiling, and HTTP tests. Use for benchmarking functions, preventing regressions, and comparing algorithms.
npx claudepluginhub intense-visions/harness-engineering --plugin harness-claudeThis skill uses the workspace's default tool permissions.
> Measure and assert on code performance using vitest bench and timing budgets
Creates and runs reliable benchmarks to measure code change impacts on performance, including latency, throughput. Supports Node.js (vitest, tinybench), Python (pytest-benchmark), frontend (Lighthouse CI), with warmup, stats.
Enforces Red-Green-Refactor TDD with correctness tests and Vitest benchmarks for performance-critical features, hot-path logic, and spec-defined requirements like <100ms response.
Optimizes Vitest performance for fast execution, watch mode, and parallelization strategies in TypeScript projects.
Share bugs, ideas, or general feedback.
Measure and assert on code performance using vitest bench and timing budgets
// sort.bench.ts
import { bench, describe } from 'vitest';
describe('sorting algorithms', () => {
const data = Array.from({ length: 10_000 }, () => Math.random());
bench('Array.sort', () => {
[...data].sort((a, b) => a - b);
});
bench('custom quicksort', () => {
quicksort([...data]);
});
});
Run with: vitest bench
it('processes 1000 items in under 100ms', async () => {
const items = createItems(1000);
const start = performance.now();
await processItems(items);
const duration = performance.now() - start;
expect(duration).toBeLessThan(100);
});
bench(
'JSON parse',
() => {
JSON.parse(largeJson);
},
{
warmupIterations: 100,
iterations: 1000,
time: 5000, // Run for at least 5 seconds
}
);
describe('string concatenation', () => {
const parts = Array.from({ length: 1000 }, (_, i) => `part${i}`);
bench('Array.join', () => {
parts.join('');
});
bench('String +=', () => {
let result = '';
for (const part of parts) result += part;
});
bench('template literal', () => {
parts.reduce((acc, part) => `${acc}${part}`, '');
});
});
it('does not leak memory over 1000 iterations', () => {
const before = process.memoryUsage().heapUsed;
for (let i = 0; i < 1000; i++) {
const result = processData(testData);
// result should be garbage collected
}
global.gc?.(); // Run with --expose-gc
const after = process.memoryUsage().heapUsed;
const growth = after - before;
expect(growth).toBeLessThan(10 * 1024 * 1024); // Less than 10MB growth
});
it('responds to /api/users in under 200ms (p95)', async () => {
const times: number[] = [];
for (let i = 0; i < 100; i++) {
const start = performance.now();
await request(app).get('/api/users').expect(200);
times.push(performance.now() - start);
}
times.sort((a, b) => a - b);
const p95 = times[Math.floor(times.length * 0.95)];
expect(p95).toBeLessThan(200);
});
// Save benchmark results to a file and compare in CI
// vitest bench --reporter=json --outputFile=bench-results.json
// vitest.config.ts
test: {
benchmark: {
include: ['**/*.bench.ts'],
reporters: ['default', 'json'],
outputFile: 'bench-results.json',
},
},
Performance testing ensures that code meets speed and resource requirements. It ranges from micro-benchmarks (single function timing) to load testing (system under concurrent traffic).
Vitest bench vs dedicated tools:
Benchmark reliability:
Statistical measures:
Common performance traps in JavaScript:
Array.join){...obj} allocates a new object)Trade-offs:
--expose-gc and is not deterministichttps://vitest.dev/guide/features.html#benchmarking