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

funcbench

v1.0.0-alpha.3

Published

A library for benchmarking JavaScript and TypeScript functions.

Downloads

13

Readme

funcbench

funcbench is a TypeScript library that helps you measure the performance of functions by executing them with different inputs and sample sizes. The main goal of thid library is to provide developers with a tool for identifying performance bottlenecks and understanding the performance characteristics of their code, and to do while providing type safety for the entire lifespan.

Features

  • Profile multiple functions with different input arguments
  • Customize the number of samples per function and input
  • Calculate mean execution time, variance, and standard deviation
  • Choose between multiple time units (e.g., seconds, milliseconds and nanoseconds)
  • Chainable builder pattern for easy configuration

Installation

To use the funcbench benchmark, first install the package from npm

npm install funcbench

Usage

First import the benchmark function from the library:

import { benchmark } from 'funcbench';

To create a new benchmark, call the benchmark function and chain the builder methods to configure it:

const addNumbers = (a: number, b: number) => a + b

const myBenchmark = benchmark<[number, number]>()
    .addFuncs(addNumbers)
    .addInputs({ name: '0 and 1', args: [0, 1] })
    .addSamples(100)
    .setUnits('ms');

You can also pass an optional configuration object to the benchmark function for initial settings:

const myBenchmark = benchmark<[number, number]>({
    functions: [addNumbers],
    inputs: [{ name: '0 and 1', args: [0, 1] }],
    samples: [100],
    units: 's'
});

Then, run the benchmark to get the results:

const results = myBenchmark.run();

The results variable will contain an array of performance statistics for each function, input, and sample size combination.

API Reference

benchmark<Args>(options?: BuilderOptions<Args>)

Creates a new Benchmark object. The optional BuilderOptions object can be used to set the initial configuration:

BuilderOptions<Args>

| Property Name | Type | Description | |---------------|-----------------------|---------------------------------------------------------------------------| | functions | Array<Function> | The functions to profile. | | inputs | Array<{ name: string, args: Args }> | The input arguments for each function. Each input must be given a name. This will appear in the output to make the results more human readable. | | samples | Array<number> | The number of samples to run for each function and input combination. | | units | 'ns' | 'ms' | 's' | The time units to use for the results (e.g., 'ms' for milliseconds). Defaults to 'ms' |

BenchmarkBuilder

The builder builder exposes several chainable methods for configuring your test:

| Method Name | Parameters | Description | |-------------------|-------------------------------------------|-------------------------------------------------------------------------------------------| | addFuncs | funcs: Function \| Function[] | Add one or more functions to the benchmark. | | addInputs | inputs: Input<Args> \| Input<Args>[] | Add one or more input argument sets to the benchmark. | | addSamples | samples: number \| number[] | Add one or more sample sizes to the benchmark. | | removeFuncs | indices: number \| number[] | Remove one or more functions from the benchmark by their indices. | | removeInputs | indices: number \| number[] | Remove one or more input argument sets from the benchmark by their indices. | | removeSamples | indices: number \| number[] | Remove one or more sample sizes from the benchmark by their indices. | | run | { rank: boolean \| undefined, testCallback: (stats: Stats) => unknown \| unknown } | Runs the benchmark and returns the results as a BenchmarkResult object. If rank is true, then the resulting stats will be sorted with priority in the following order: sample size ascending, input name alphabetic sorting and then mean execution time. | | setFuncs | funcs: Function[] | Set the functions to profile. | | setInputs | inputs: Input<Args>[] | Set the input arguments for each function. | | setSamples | samples: number[] | Set the number of samples to run for each function and input combination. | | setUnits | units: 'ns' | 'ms' | 's' | Set the time units to use for the results (e.g., 'ms' for milliseconds). |

Once you have set up the benchmark how you want it to be, just call the run function and handle the result.

BenchmarkResult

An object that contains an array of Stats objects (one for each function/input/sample combination). A Stats has the following properties.

| Property Name | Type | Description | |---------------|-----------------------|---------------------------------------------------------------------------| | funcName | string | The name of the function passed in. If you passed in an anonymous function, this will be an empty string "". | inputName | string | The string representing the name of the input passed in during the build process. | samples | number | The sample size of this dataset. | mean | number | The mean of all runs of the functions in the sample set for a given input. | sigma | number | The standard deviation of all runs of the functions in the sample set for a given input. | sigmaSquared | number | The variance of all runs of the functions in the sample set for a given input.

Contact

If you have any questions for me, feel free to contact me through my website.