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

microprofiler

v2.0.0

Published

Low-level profiling for node.js: squeeze those microseconds.

Downloads

1,213

Readme

NPM

microprofiler

Low-level profiling for node.js: squeeze those microseconds!

Microprofiler allows you to measure performance of your code down to the level of the microsecond, allowing micro-optimizations that are unfeasible any other way.

Instead of blind profiling your entire code, insert probes whenever needed and measure only at interesting points.

Installation

Simply install from npm:

$ npm install microprofiler

Or add to your package.json as a dependency.

Compatibility

Versions 2 and later should be used with Node.js v16 or later:

  • Node.js v16 or later: ^2.0.0.
  • Earlier Node.js: please stick to ^1.0.0.

Usage

The library has been designed to be dead simple to use. First import microprofiler:

import microprofiler from 'microprofiler';

Locate a portion of code you want to measure. Insert a measurement before it starts:

const start = microprofiler.start();

The function microprofiler.start() is simply an alias to process.hrtime(), which returns time with nanosecond precision.

To simply measure the number of microseconds elapsed in the block of code, insert a call to measureFrom() passing only the start:

const elapsedUs = microprofiler.measureFrom(start);

Optionally you may want the microprofiler to keep track of time, then pass a key as the optional second parameter:

microprofiler.measureFrom(start, 'code');

To show current stats for a given key on demand, use microprofiler.show():

microprofiler.show('code');

A line similar to this will be shown:

[Tue Mar 04 2014 01:40:17 GMT+0100 (CET)] INFO Profiling code: 1000 requests, mean time: 105.76 µs, rps: 9455

Even simpler, you can also pass an optional number of requests after which stats will be shown:

microprofiler.measureFrom(start, 'code', 1000);

Now the stats will be shown automatically after 1000 requests.

That is really all there is to it. The functions above can be used in asynchronous code without problems.

Measuring Performance

Each measurement should take less about one microsecond to perform. (It would be better to have it take less than one microsecond, but just calling process.hrtime() twice takes longer than that so it is not possible.) The library is therefore accurate to within two microseconds.

Given that the impact of profiling is low but not zero, the whole module can be disabled with microprofiler.disable():

microprofiler.disable();

To get stats from your code just call microprofiler.getStats():

const stats = microprofiler.getStats('code');

It will return an object with current stats info, like this:

{
    "key":"code",
    "requests":10000,
    "timeUs":9193.090999999931,
    "meanTimeUs":"0.92",
    "rps":1087773
}

Example

Suppose we want to measure how long the following bit of code takes.

let string = '';
let total = 0;
for (let i = 0; i < 100; i++)
{
    string += i;
    total += parseInt(string);
}

We can just add a couple of lines before and after it:

import microprofiler from 'microprofiler';

const start = microprofiler.start();
... [profiled code goes here]
microprofiler.measureFrom(start, 'loop', 10000);

Now every time that code is executed a measure is taken and stored; after 10000 runs the microprofiler will show gathered results.

Multiple measurements can be taken:

import microprofiler from 'microprofiler';

const start = microprofiler.start();
... [first block of profiled code goes here]
microprofiler.measureFrom(start, 'first', 10000);
... [more profiled code]
microprofiler.measureFrom(start, 'second', 10000);
... [even more profiled code]
microprofiler.measureFrom(start, 'third', 10000);

Now two intermediate measurements are taken.

To run in a synthetic test just stick the whole thing in a second loop:

import microprofiler from 'microprofiler';

const rounds = 10000;
for (let index = 0; index < rounds; index++)
{
    const start = microprofiler.start();
    ... [profiled code goes here]
    microprofiler.measureFrom(start, 'loop', rounds);
}

It will run the code, profile it, and show the results at the end.

Also see the sample in the repo.

License

(The MIT License)

Copyright (c) 2013-2023 Alex Fernández [email protected]

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.