* Runs a performance test for a given function * @param {string} testName - Name of the test * @param {Function} testFunction - Function to benchmark * @param {number} iterations - Number of iterations to run * @returns {Object} Performance results
(testName, testFunction, iterations = ITERATIONS)
| 17 | * @returns {Object} Performance results |
| 18 | */ |
| 19 | function benchmark(testName, testFunction, iterations = ITERATIONS) { |
| 20 | // Warmup |
| 21 | for (let i = 0; i < WARMUP_ITERATIONS; i++) { |
| 22 | testFunction(); |
| 23 | } |
| 24 | |
| 25 | // Actual benchmark |
| 26 | const startTime = process.hrtime.bigint(); |
| 27 | |
| 28 | for (let i = 0; i < iterations; i++) { |
| 29 | testFunction(); |
| 30 | } |
| 31 | |
| 32 | const endTime = process.hrtime.bigint(); |
| 33 | const totalTime = Number(endTime - startTime) / 1000000; // Convert to milliseconds |
| 34 | const avgTime = totalTime / iterations; |
| 35 | const opsPerSecond = 1000 / avgTime; |
| 36 | |
| 37 | return { |
| 38 | testName, |
| 39 | iterations, |
| 40 | totalTime: totalTime.toFixed(2), |
| 41 | avgTime: avgTime.toFixed(6), |
| 42 | opsPerSecond: Math.round(opsPerSecond), |
| 43 | }; |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * Prints benchmark results in a formatted table |