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

reality-check

v1.0.0

Published

A unit-test like wrapper around benchmark.js, for testing CPU performance of functions in JavaScript or TypeScript

Downloads

6

Readme

Reality Check

A unit-test like wrapper around benchmark.js, for testing CPU performance of functions in JavaScript or TypeScript

Installation

npm install reality-check

Focusing on Side-By-Side Tests

The benchmark.js is the core engine powering this tool. Benchmark.js is a powerful tool with many hook-ins, statistics and possibilities for creating a benchmark performance report. For the sake of this project, the number of operations-per-second and number-of-times-executed are the basis of the benchmark report.

This project focuses on creating a way to generate that kind of report for side-by-side comparisons of different ways of executing code. As this is a very common reason for running a CPU performance benchmark - comparing "my way" vs "your way" - the whole of Benchmark.js is being reduced down into a format that allows you to see those kinds of side-by-side comparisons.

Aiming for an API similar to Unit Testing

Unit test frameworks in JavaScript/TypeScript provide us a way to execute chunks of code in an entire run (organized into different test "suites"). This convenience makes it much easier to check the validity of our code. Likewise, this project here aims to provide a "unit test like" wrapper around Benchmark.js and the side-by-side CPU performance comparisons. Writing a benchmark test should therefore remind you of writing unit tests.

Usage

This project has a CLI component to it and it executes against any file where you've imported/required from reality-check.

So in your test file you may have something like this:

const { test } = require("reality-check")

test("Squaring each number in a list", benchmark => {
    const unsortedArray = Array(10000)
        .fill(0)
        .map((_, i) => Math.floor(Math.random() * i))

    benchmark(() => {
        const arrCopy = Array(10000)
        unsortedArray.forEach((val, i) => {
            arrCopy[i] = val**2
        })
        return arrCopy
    }, "Array.prototype.forEach")

    benchmark(() => {
        const arrCopy = []
        for (const val of unsortedArray) {
            arrCopy.push(val**2)
        }
        return arrCopy
    }, "for..of")

    benchmark(() => {
        const arrCopy = Array(10000)
        for (let i = 0; i < 10000; i++) {
            arrCopy[i] = unsortedArray[i]**2
        }
        return arrCopy
    }, "for loop")
})

Now you can invoke the CLI and it'll find your test and execute it:

$ rcheck

Running benchmark test suite: "Squaring each number in a list" . . .

[Array.prototype.forEach]
  - 8,863 ops/sec
  - 1,637 times executed
  - 95 runs sampled
[for..of]
  - 5,755 ops/sec
  - 292 times executed
  - 96 runs sampled
[for loop]
  - 38,975 ops/sec
  - 1,978 times executed
  - 96 runs sampled

⏱ Fastest is for loop 🚀
   - Array.prototype.forEach .... 339.73% slower!
   - for..of .................... 577.21% slower!

Glob Filtering Tests

You can specify any glob pattern you wish to use to filter tests:

$ rcheck *.benchmark-test.js benchmarks/

Otherwise, this tool will look for and run any file importing from the reality-check library starting from the cwd.

Additional CLI Options

And, these are some additional named CLI options:

  • --require - One or more commonjs modules to import prior to running the test
  • --debug - Turn on debug logging (for troubleshooting the script itself)
  • --cwd - Optional base directory from which to search for benchmark tests (defaults to process.cwd())

Each Test Must Return the Same Value

When benchmarking, if a function errors out it'll look like it ran faster than other functions being benchmarked. A lot of time can be wasted if you're not aware of this problem. To mitigate this issue, this project ensures that every function being benchmarked returns the same value.

When writing a test around a function which does not return a value, consider wrapping it so that the result of each function produces a value. The example listed in the Usage section demonstrates this around a test of a forEach/for/for..of loop.