npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@shvmerc/performance

v0.1.0-alpha

Published

A JavaScript tool to compare the performance difference between different algorithms, functions or versions of the same code.

Downloads

1

Readme

Performance JS

A JavaScript tool to compare the performance difference between different algorithms, functions or versions of the same code.

How it works ?

The CodeRunner class is used to group the code blocks, then execute them several times, while calculating the average execution time for each one.

Usage

Import and initialize an instance of the CodeRunner class.

const { CodeRunner } = require('@shvmerc/performance');
const runner = new CodeRunner();

Add the code blocks using the add method, you must enter the following parameters.

  • id: Name or ID for the code block, must be unique.
  • fn: The function that contains the code to be executed.
runner.add('Method 1', () => {
  const array: number[] = [];
  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 as number[];
});

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

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

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

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

A larger number of repetitions results in a more accurate comparison, but can significantly increase the total duration.

const performances = runner.run(100000);

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

First, create a new instance of the PerformancePrinter class, you must enter the results directly as the constructor argument.

const { PerformancePrinter } = require('@shvmerc/performance');
const printer = new PerformancePrinter(performances);

Execute the print method to display a comparative graph of the results.

Multiple options are available to configure the output graph.

  • options.target: Displays the percentage difference between the results and the target. By default, the target will be the lowest average result.
  • options.sort: Sort the results, for 'default' by index and for 'ascending' or 'descending' by average time.
  • options.highlight: Highlights selected code blocks in the results.
printer.print({
  target: 'Method 4',
  sort: 'ascending',
  highlight: {
    red: 'Method 2',
    green: 'Method 4',
  },
});