(options = {})
| 10 | * @param {{benchmarksCount?: number, iterationsCount?: number}} options |
| 11 | */ |
| 12 | let makeBenchmarkSuite = (options = {}) => { |
| 13 | const benchmarkTimings = {}; |
| 14 | const benchmarksCount = options.benchmarksCount || 1000; |
| 15 | const iterationsCount = options.iterationsCount || 100000; |
| 16 | const testCases = []; |
| 17 | |
| 18 | const suite = {}; |
| 19 | /** |
| 20 | * @param {string} title |
| 21 | * @param {(i: number) => void} fn |
| 22 | */ |
| 23 | suite.add = (title, fn) => { |
| 24 | testCases.push({ title, fn }); |
| 25 | return suite; |
| 26 | }; |
| 27 | suite.run = () => { |
| 28 | for ( |
| 29 | let benchmarkIndex = 0; |
| 30 | benchmarkIndex < benchmarksCount; |
| 31 | benchmarkIndex++ |
| 32 | ) { |
| 33 | testCases.forEach((testCase) => { |
| 34 | const description = testCase.title + '(' + iterationsCount + 'x)'; |
| 35 | const start = performance.now(); |
| 36 | for (let i = 0; i < iterationsCount; i++) { |
| 37 | testCase.fn(i); |
| 38 | } |
| 39 | benchmarkTimings[description] = benchmarkTimings[description] || []; |
| 40 | benchmarkTimings[description].push(performance.now() - start); |
| 41 | }); |
| 42 | } |
| 43 | |
| 44 | const results = {}; |
| 45 | for (let benchmarkName in benchmarkTimings) { |
| 46 | results[benchmarkName] = |
| 47 | benchmarkTimings[benchmarkName].reduce((sum, value) => sum + value, 0) / |
| 48 | benchmarksCount; |
| 49 | } |
| 50 | return results; |
| 51 | }; |
| 52 | return suite; |
| 53 | }; |
| 54 | |
| 55 | module.exports = { makeBenchmarkSuite }; |
no test coverage detected