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

picobench

v0.0.3

Published

Library for testing performance of functions

Downloads

24

Readme

picobench

A pico (very small) library for testing performance of functions. Meant to be used in combination with a unit test framework like Vitest or Jest

ci npm-publish npm version

Note for TypeScript users

If you are using TypeScript, you should consider transpiling your project first, and then running the benchmark against the transpiled code. This will give a more precise result.

Installation

npm i -D picobench

Usage

To benchmark function you should use benchSync for synchronous code, and benchAsync for asynchronous code.

The output of the timing functions look like the following:

interface IBenchResult {
    /**
     * @summary operations per seconds
     * @calculation 1 / (average_in_ms / 1000)
     */
    ops: number;
    /**
     * @summary averate time to run in milliseconds
     */
    average: number;
    /**
     * @summary median time to run in milliseconds
     */
    median: number;
    /**
     * @summary slowest run in milliseconds
     */
    minimum: number;
    /**
     * @summary fastest run in milliseconds
     */
    maximum: number;
    /**
     * @summary all runs in milliseconds
     */
    runs: number[];
}

Synchronous code

import { benchSync } from 'picobench';
// or
const { benchSync } = require('picobench');
// or
const benchSync = require('picobench').benchSync;

function dummyFn() {
    let sum = 0;
    for (let i = 0; i < 1000000; i += 1) sum += 1;
    return sum;
}

const benchmarkResult = benchSync(dummyFn);

//  {
//      ops: 1052.6315789473683,
//      average: 0.95,
//      median: 0.9604999999999999,
//      minimum: 0.903,
//      maximum: 0.97,
//      runs: [0.903, 0.908, 0.951, 0.958, 0.96, 0.961, 0.961, 0.964, 0.964, 0.97]
//  }

Synchronous code

import { benchSync } from 'picobench';
// or
const { benchAsync } = require('picobench');
// or
const benchAsync = require('picobench').benchSync;

function dummyFn(n = 1000000): number {
    let sum = 0;
    for (let i = 0; i < n; i += 1) sum += 1;
    return sum;
}

// if the function doesn't take parameters you can call it like this
const benchmarkResult = benchSync(dummyFn);
// otherwise you will have to call it with a callback function
const benchmarkResult = benchSync(() => dummyFn(10000));

//  {
//      ops: 1052.6315789473683,
//      average: 0.95,
//      median: 0.9604999999999999,
//      minimum: 0.903,
//      maximum: 0.97,
//      runs: [0.903, 0.908, 0.951, 0.958, 0.96, 0.961, 0.961, 0.964, 0.964, 0.97]
//  }

Asynchronous code

import { benchAsync } from 'picobench';
// or
const { benchAsync } = require('picobench');
// or
const benchAsync = require('picobench').benchAsync;

async function sleep(ms = 500) {
    return new Promise((resolve) => setTimeout(resolve, ms));
}

// if the function doesn't take parameters you can call it like this
const benchmarkResult = await benchAsync(sleep);
// otherwise you will have to call it like a callback function
const benchmarkResult = await benchAsync(() => await sleep(500));

//  {
//      ops: 1.996369801153583,
//      average: 500.9091999999999,
//      median: 500.87800000000004,
//      minimum: 500.796,
//      maximum: 501.292,
//      runs: [500.796, 500.823, 500.851, 500.869, 500.877, 500.879, 500.894, 500.898, 500.913, 501.292]
//  }

Callbacks

If you wish to benchmark a function that needs parameters, you have to pass the function as a callback function.

import { benchSync, benchAsync } from 'picobench';

const syncBenchmark = benchSync(() => {
    callSomeSyncFunction('param1', 'param2');
});

const asyncBenchmark = await benchAsync(async () => {
    await callSomeAsyncFunc('param1', 'param2');
});

The same goes if you wish to benchmark code that isn't wrapped in a function already.

import { benchSync } from 'picobench';

const syncBenchmark = benchSync(() => {
    const numbers = 1000;
    Array.from({ length: numbers }, () => Math.floor(Math.random() * numbers));
});