Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/dikendev/npm-benchmark-ts
Benchmark and Data Visualization package
https://github.com/dikendev/npm-benchmark-ts
algorithms chart data-visualization javascript jest library npm-package typescript typescript-library
Last synced: about 1 month ago
JSON representation
Benchmark and Data Visualization package
- Host: GitHub
- URL: https://github.com/dikendev/npm-benchmark-ts
- Owner: Dikendev
- Created: 2024-03-17T21:25:02.000Z (10 months ago)
- Default Branch: main
- Last Pushed: 2024-05-23T00:36:18.000Z (8 months ago)
- Last Synced: 2024-05-23T03:25:44.151Z (8 months ago)
- Topics: algorithms, chart, data-visualization, javascript, jest, library, npm-package, typescript, typescript-library
- Language: TypeScript
- Homepage: https://www.npmjs.com/package/npm-benchmark-ts
- Size: 376 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Benchmark and Data Visualization
This project was created to measure the execution time of algorithms. It's worth mentioning that shorter execution times don't necessarily is the best solution, however they provide a valuable metric for our projects. With this package, I aim to demonstrate how we can effectively measure execution time using minimal external libraries.
## Installation
To install all the dependencies, use the package manager [npm](https://www.npmjs.com/) and use the follow:
```bash
npm i npm-benchmark-ts
```The unique external library in this project is the [ChartJsImage](https://www.npmjs.com/package/chartjs-to-image/).
This library provides a ChartJsImage object. Import it, instantiate it, and set the necessary config.## Usage
First need to import :
```typescript
import { benchMark, BenchmarkFunctions } from "npm-benchmark-ts";
```For example, this method uses a for loop to sum an array of numbers:
```typescript
function sumNumberUsingFor(numbers: number[]): number {
let result: number = 0;
for (let i = 0; i < numbers.length; i++) {
result += numbers[i];
}
return result;
}
```And this method sums an array of numbers using the reduce built-in method in TypeScript:
```typescript
function sumNumberUsingReduce(numbers: number[]): number {
return numbers.reduce((acc, current) => acc + current, 0);
}
```To compare the execution time, run the test to generate the image and JSON files as follows:
```typescript
const arrayLength = 1000000;
const numberArray = generateArray(arrayLength);const benchmark1: BenchmarkFunctions = {
functionDescription: "forLoop",
functionUnderTest: () => SumMethods.sumNumberUsingFor(numberArray),
detail: "Sum numbers using for",
};const benchmark2: BenchmarkFunctions = {
functionDescription: "reduce",
functionUnderTest: () => SumMethods.sumNumberUsingReduce(numberArray),
detail: "Sum numbers using reduce",
};await benchMark("comparison_sum_methods", [
benchmark1,
benchmark2,
]);
```## Chart Image Benchmark Result
If you want to generate a chart and json response you can pass the option parameter.
```typescript
const options: Options = {
dirPath: "TMP",
};const benchMarkResult = await benchMark(
"comparison_sum_methods",
[benchmark1, benchmark2],
options
);
```## Chart Data Result
![chart image](/TMP/comparison_sum_methods.png "Result chart png image")
## JSON Data Result
```json
{
"forLoop": {
"name": "forLoop",
"duration": 4.217916999012232
},
"reduce": {
"name": "reduce",
"duration": 14.500208999961615
}
}
```## Conclusion
Based on the benchmark results, it's evident that in this specific scenario, the method sumNumberUsingFor, which utilizes a for loop, is better than sumNumberUsingReduce, which use the built-in reduce method, in terms of execution time. The for loop method completes the task in approximately 4.218 milliseconds, while the reduce method takes around 14.500 milliseconds.
However, it's important to note that the superiority of the for loop in this scenario may vary depending on different factors such as the size of the input array, the complexity of the operations within the methods, and the specific environment in which the code is executed. Therefore, while these benchmark results provide valuable insights into the performance of these methods under certain conditions, it's crucial to consider various scenarios and factors when determining the optimal approach for a given task.
## Contributing
Pull requests are welcome <3. Please make sure to update tests as appropriate.
## License
[MIT](https://choosealicense.com/licenses/mit/)