Created
September 6, 2023 22:22
-
-
Save douglascayers/444dfee877dd76c3c5a6c5ab038d85c3 to your computer and use it in GitHub Desktop.
Benchmark an operation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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