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

bench

v0.3.6

Published

A little utility for doing side-by-side benchmarks in nodejs

Downloads

675

Readme

node bench

A little utility for doing side-by-side benchmarks in nodejs.

This is not for benchmarking your HTTP servers. Use ab for that.

Installation

npm install bench

Usage

Write your script like this:

exports.compare = {
  "function wrapper" : function () {
    var x = (function (a) {
      return a;
    })("foo");
  },
  "with(){} wrapper" : function () {
    var x;
    with ({a : "foo"}) {
      x = a;
    }
  }
  "no wrapper" : function () {
    var a = "foo";
    var x = a;
  }
};
require("bench").runMain()

Then, start it up with node.

$ node my-test-script.js

It'll output the scores in processes/ms, so a higher score is always better. That is, the values are kHz, not Hz.

You can also export time, count, and comparecount to change the behavior slightly.

Your test script is just a plain old commonjs module, so it can include other things, update require.paths, whatever setup you need to do. Generally, it's a good idea to do this stuff in the module itself, rather than in the comparison functions, so that you can better isolate the units that you want to test.

Fields

Export the following fields from your benchmark script.

compare - The hash of functions that will be compared. The output will use the object key as the title. They're called without any arguments, in the default scope. It's assumed that you should know how to make this do whatever you need it to.

time - How long (in ms) to run the tests for. A higher value will result in more accurate tests that take longer to run. Default: 1000

compareCount - How many times to do the test runs. This should be some fairly small number. Tests are run multiple times in varying order to average out the variation due to calling one function first, a primed cache, garbage collection, etc. Higher value = more accurate, slower tests. Default: 8

countPerLap - Especially when doing asynchronous benchmarking, you may want to structure your functions so that they run a bunch of times before continuing. In these cases, to make your scores reflect the actual number of processes per ms, indicate the number of runs per call in the "countPerLap" field. Default: 1

done - A function that will be called with the results of the runs when they're complete. By default, this calls a function that will analyze the results a bit and write the data to stdout.

Asynchronous Benchmarking

Just write your functions so that they take a single argument. That argument is your callback. Have fun with it.

Your callback will be fired using process.nextTick. This has a wee bit of overhead, so if you're testing something really fast, you should probably construct it to run many times before calling the callback. Check the examples/nexttick-vs-settimeout.js test for an example.

WARNING!

Statistics are powerful tools, and in the wrong hands, can lead to a lot of mayhem. Please use this tool for good, and not evil.