Skip to content

shvmeless/performance-js

Repository files navigation

Performance JS

A JavaScript tool to compare the time performance difference between algorithms and functions.

Usage

Import and initialize an instance of the FunctionRunner class.

const { FunctionRunner } = require('@shvmeless/performance');
const runner = new FunctionRunner();

If you need it, you can add an instance of a class that implements the ProgressVisualizer interface, this will make FunctionRunner to show its progress in real time.

This package includes the ConsoleProgressVisualizer, which will print the progress in the console.

const { ConsoleProgressVisualizer } = require('@shvmeless/performance');
const { FunctionRunner } = require('@shvmeless/performance');

const visualizer = new ConsoleProgressVisualizer();
const runner = new FunctionRunner(visualizer);

Add the functions you want to compare using the add method, passing the name of the function and the function itself.

runner.add('Method 1', () => {
  const array = [];
  for (let i = 0; i <= 100; i++) {
    array.push(i);
  }
  return array;
});

runner.add('Method 2', () => {
  const array = Array(101);
  for (let i = 0; i <= 100; i++) {
    array[i] = i;
  }
  return array;
});

runner.add('Method 3', () => {
  return Array(101).map((_, index) => index);
});

runner.add('Method 4', () => {
  return Array.from({ length: 101 }, (_, index) => index);
});

runner.add('Method 5', () => {
  return [...Array(101).keys()];
});

Run the comparison using the run method and passing the number of iterations.

A larger number of iterations results in a more accurate comparison, but can significantly increase the total execution time.

const performances = runner.run(100000);

Now you can use the results as you wish, but this package includes a simple way to print the results into the console.

First, create a new instance of the PerformanceVisualizer class.

const { PerformanceVisualizer } = require('@shvmeless/performance');
const printer = new PerformanceVisualizer(performances);

Then, call one of the following methods to print the results.

  • printMeanValues: Prints a graph representation of the mean values of each function.
  • printValueRanges: Prints a graph representation of the value min, mean and max values of each function.
  • printTotalTime: Print a graph of the total sum of each function.

About

A JavaScript tool to compare the time performance difference between algorithms and functions.

Resources

License

Stars

Watchers

Forks

Packages

No packages published