Skip to content

Commit

Permalink
Add benchmark utility function and update package version
Browse files Browse the repository at this point in the history
  • Loading branch information
0pilatos0 committed Jan 27, 2024
1 parent 748e6f3 commit 12e6763
Show file tree
Hide file tree
Showing 7 changed files with 108 additions and 1 deletion.
22 changes: 22 additions & 0 deletions USAGE.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ Here are some of the utility functions and classes available in this library:

## Table of Contents

- [BenchmarkUtils](#benchmarkutils)
- [benchmark](#benchmark)

- [ArrayUtils](#arrayutils)
- [sample](#sample)
- [shuffle](#shuffle)
Expand Down Expand Up @@ -80,6 +83,25 @@ Here are some of the utility functions and classes available in this library:
- [truncate](#truncate)


## BenchmarkUtils

the following utility functions are available in the `BenchmarkUtils` class, they can be used as a static method on the `BenchmarkUtils` class or as a standalone function.


### benchmark
```typescript
/**
* 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.
*/
```



## ArrayUtils

the following utility functions are available in the `ArrayUtils` class, they can be used as a static method on the `ArrayUtils` class or as a standalone function.
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "typelegend",
"version": "0.1.8",
"version": "0.1.9",
"module": "dist/index.mjs",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
44 changes: 44 additions & 0 deletions src/benchmark/benchmark.ts
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,
};
}
9 changes: 9 additions & 0 deletions src/benchmark/index.ts
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";

1 change: 1 addition & 0 deletions src/function/compose.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ export function compose<T>(...fns: ((arg: T) => T)[]): (arg: T) => T {
return function (x: T): T {
return fns.reduceRight((v, f) => f(v), x);
};
console.log("Hello World:")
}
1 change: 1 addition & 0 deletions src/index.ts
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";
Expand Down
30 changes: 30 additions & 0 deletions test/benchmark/benchmark.test.ts
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);
});
});

0 comments on commit 12e6763

Please sign in to comment.