Skip to content

Instantly share code, notes, and snippets.

@douglascayers
Created September 6, 2023 22:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save douglascayers/444dfee877dd76c3c5a6c5ab038d85c3 to your computer and use it in GitHub Desktop.
Save douglascayers/444dfee877dd76c3c5a6c5ab038d85c3 to your computer and use it in GitHub Desktop.
Benchmark an operation
function benchmarkLoop(iterations: number, operation: () => void): void {
const times: number[] = [];
for (let i = 0; i < iterations; i++) {
const start = performance.now();
operation();
const end = performance.now();
times.push(end - start);
}
const shortestTime = Math.min(...times);
const longestTime = Math.max(...times);
const totalTime = times.reduce((acc, time) => acc + time, 0);
const averageTime = totalTime / iterations;
console.log(`Shortest Time (ms): ${shortestTime}`);
console.log(`Longest Time (ms): ${longestTime}`);
console.log(`Average Time (ms): ${averageTime}`);
console.log('');
}
const iterations = 5000;
benchmarkLoop(iterations, () => {
// TODO
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment