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

@jsperf.dev/benchmark

v0.3.4

Published

A tap-like benchmarking library

Downloads

2

Readme

@jsperf.dev/benchmark

Getting Started

This package exports an initialized instance of the Benchmark class from @jsperf.dev/core. It is initialized with all configuration properties set to their documented defaults.

Import with const benchmark = require('@jsperf.dev/benchmark').default for CJS, or import benchmark from '@jsperf.dev/benchmark' for ESM

Start the benchmarking scipt using one of the import methods:

const benchmark = require("@jsperf.dev/benchmark").default;

Some configuration values can be modified. Make sure this happens within the same execution step of the main script. The configurable properties can be found in the @jsperf.dev/core docs under Benchmark - Instance Properties.

For example, the sumOfSquares example sets its meta property:

const N = 10000;

benchmark.meta = {
  title: "Sum of Squares",
  description: `Compare 3 seperate algorithms for calculating the sum of the square of all numbers from 0 to ${N}`,
};

Then, begin by defining necessary lifecycle methods. The four available are beforeAll, beforeEach, afterAll, and afterEach.

A benchmark suite has a context that is passed to every lifecycle method and run scripts. The recommended way to set the context is during the beforeAll lifecycle method.

For example, the context should contain normalized input values or methods that all of the run scripts should use.

From the sumOfSquares example:

benchmark.beforeAll((context) => {
  Object.assign(context, {
    list: Array.from({ length: 100 }, (_, i) => i),
    square: (n) => n ** 2,
  });
});

Similarly, the after lifecycle methods can be used to assert that the results of each run script are correct. Continuing with the sumOfSquares example, the following block asserts that the list in the context remains the same length, and that the result from each run is the expected value.

benchmark.afterEach(({ list }, sum) => {
  assert(list.length === 100, `list has 100 items`);
  assert(sum === 328350, `sum is ${sum}`);
});

Each run should be designed to make use of the values in context, and then be sure to return the computed result so it can be verified in the after blocks.

// examples/sumOfSquares/reduce.js
module.exports.default = ({ list, square }) => {
  return list.reduce((acc, i) => (acc += square(i)), 0);
};

Finally, back in the benchmarking script, add runs using the run method. The first argument must be a unique identifier, and the second argument must be the absoulte path to the run script. The sumOfSquares example is written in CommonJS so it makes use of path.resolve() and __dirname, but ESM users can make use of a path resolver like: path.resolve(path.dirname(url.fileURLToPath(import.meta.url)), 'reduce.js').

benchmark.run("reduce", path.resolve(__dirname, "reduce.js"));

The benchmark script can be executed using node and information about the execution will be logged to stdout. Below is a simplified output of the sumOfSquares example.

{ "script": "Sum of Squares" }
{ "description": "Compare 3 seperate algorithms for calculating the sum of the square of all numbers from 0 to 100" }
{ "samples": 10}
{ "results": [
    { "run": "quick sum", "medianTime": 0.010526984930038452 },
    { "run": "forEach", "medianTime": 0.010573983192443848 },
    { "run": "reduce", "medianTime": 0.01349899172782898 }
]}

API

Default: benchmark

  • type: Benchmark

Function: setLogger(logger)

Arguments:

Use this method to override the default pino logger instance.

Testing

Execute tests using pnpm test