-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add benchmark utility function and update package version
- Loading branch information
Showing
7 changed files
with
108 additions
and
1 deletion.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/** | ||
* Measures the execution time of a function by running it multiple times. | ||
* @param fn The function to benchmark. | ||
* @param iterations The number of times to run the function (default: 1). | ||
* @param log Whether to log the benchmark result (default: true). | ||
* @param args Additional arguments to pass to the function. | ||
* @returns An object containing the total execution time and average execution time. | ||
*/ | ||
export function benchmark( | ||
fn: Function, | ||
iterations: number = 1, | ||
log: boolean = true, | ||
...args: any[] | ||
): { | ||
total: number; | ||
average: number; | ||
} { | ||
let executionPerIteration: number[] = []; | ||
const start = performance.now(); | ||
|
||
for (let i = 0; i < iterations; i++) { | ||
const start = performance.now(); | ||
fn(...args); | ||
const end = performance.now(); | ||
executionPerIteration.push(end - start); | ||
} | ||
const end = performance.now(); | ||
|
||
const averageExecutionTime = | ||
executionPerIteration.reduce((a, b) => a + b, 0) / iterations; | ||
|
||
if (log) { | ||
console.log( | ||
`${fn.name} took ${ | ||
end - start | ||
} milliseconds to complete ${iterations} iterations.` | ||
); | ||
} | ||
|
||
return { | ||
total: end - start, | ||
average: averageExecutionTime, | ||
}; | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
|
||
import { benchmark } from "./benchmark"; | ||
|
||
export class BenchmarkUtils { | ||
static benchmark: typeof benchmark = benchmark; | ||
} | ||
|
||
export { benchmark } from "./benchmark"; | ||
|
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export * from "./benchmark"; | ||
export * from "./array"; | ||
export * from "./date"; | ||
export * from "./function"; | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { describe, expect, it } from "bun:test"; | ||
import { benchmark } from "../../src/benchmark/benchmark"; | ||
|
||
describe("benchmark", () => { | ||
it("should measure the execution time of a function", () => { | ||
// Test case 1: Measure the execution time of a simple function | ||
const fn1 = () => { | ||
// Some code to be measured | ||
}; | ||
const result1 = benchmark(fn1); | ||
expect(result1.total).toBeGreaterThan(0); | ||
expect(result1.average).toBeGreaterThan(0); | ||
|
||
// Test case 2: Measure the execution time of a function with arguments | ||
const fn2 = (a: number, b: number) => { | ||
// Some code to be measured | ||
}; | ||
const result2 = benchmark(fn2, 10, true, 5, 10); | ||
expect(result2.total).toBeGreaterThan(0); | ||
expect(result2.average).toBeGreaterThan(0); | ||
|
||
// Test case 3: Measure the execution time of a function without logging | ||
const fn3 = () => { | ||
// Some code to be measured | ||
}; | ||
const result3 = benchmark(fn3, 5, false); | ||
expect(result3.total).toBeGreaterThan(0); | ||
expect(result3.average).toBeGreaterThan(0); | ||
}); | ||
}); |