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

@jalik/benchmark

v2.0.3

Published

Utilities to benchmark code.

Downloads

5

Readme

@jalik/benchmark

GitHub package.json version Build Status GitHub GitHub last commit GitHub issues npm

Features

  • Functions to measure a single function
  • Functions to measure and compare different functions
  • Works with sync and async code
  • Included functions to format and display results in the console

Measure a single function

To measure a single synchronous function, use measureSync(func, iterations).

import { measureSync } from '@jalik/benchmark';

function logHelloWorld () {
  console.log('hello world');
}

// Run function 1000 times
const result = measureSync(logHelloWorld, 1000);

To measure an asynchronous function, use measure(asyncFunc, iterations).

import { measure } from '@jalik/benchmark';

function logHelloWorld () {
  setTimeout(() => {
    console.log('hello world')
  }, 2000);
}

// Run function 100 times
measure(logHelloWorld, 100).then((result) => {
  // do something with the result...
});

The result object of a measure looks like this:

interface MeasureResult {
  average: number;
  fastest: number;
  ips: number;
  ipsAccuracy: number;
  ipsRounded: number;
  iterations: number;
  median: number;
  slowest: number;
  total: number;
}

You can show measure result in the console with logMeasureResult(result).

import { logMeasureResult } from '@jalik/benchmark';

const test = () => console.log("Hello World");
const iterations = 1000;

// sync version.
const result1 = measureSync(test, iterations);
logMeasureResult(result1);

// async version.
measure(test, iterations).then((result2) => {
  logMeasureResult(result2);
});
iterations/s: 82 ±-0.36%
total: 1224.95 ms
average: 12.25 ms
median: 11.93 ms
fastest: 11.58 ms
slowest: 24.43 ms

Measure several functions

To measure several synchronous functions, use benchmarkSync(jobs, iterations).

import { benchmarkSync } from '@jalik/benchmark';

function incrementPlusPlus () {
  for (let i = 0; i < 10000; i++) {
    // do something
  }
}

function incrementPlusEqual () {
  for (let i = 0; i < 10000; i += 1) {
    // do something
  }
}

const jobs = {
  incrementPlusPlus,
  incrementPlusEqual,
};

// Run each function 1000 times 
const result = benchmarkSync(jobs, 1000);

To measure several asynchronous functions, use benchmark(jobs, iterations).

import { benchmark } from '@jalik/benchmark';

function job1 () {
  // return promise...;
}

function job2 () {
  // return promise...;
}

const jobs = {
  job1,
  job2,
};

// Run each function 1000 times 
benchmark(jobs, 1000).then((result) => {
  // do something with the result
});

The result object of a benchmark looks like this:

interface BenchmarkResult {
  [key: string]: MeasureResult;
}

You can show benchmark result in the console with logBenchmarkResult(result).

import {
  benchmark,
  benchmarkSync,
  logBenchmarkResult
} from '@jalik/benchmark';

// sync version
const result1 = benchmarkSync({
  doSomethingSlow: () => { /* ... */ },
  doSomethingFast: () => { /* ... */ },
}, 1000);

logBenchmarkResult(result1);

// async version
benchmarkSync({
  doSomethingSlow: () => { /* ... */ },
  doSomethingFast: () => { /* ... */ },
}, 1000).then((result2) => {
  logBenchmarkResult(result2);
});
#1 doSomethingFast
iterations/s: 1250 ±0.00%
total: 8.00 ms
average: 0.80 ms
median: 1.00 ms
fastest: 0.00 ms
slowest: 1.00 ms

#2 doSomethingSlow
iterations/s: 40 ±0.00%
total: 250.00 ms
average: 25.00 ms
median: 25.00 ms
fastest: 24.00 ms
slowest: 27.00 ms

Changelog

History of releases is in the changelog.

License

The code is released under the MIT License.