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

performance-stopwatch

v2.2.1

Published

A stopwatch timer to track processing time between and during function calls

Downloads

189

Readme

Performance Stopwatch

A simple tool to help detect performance bottlenecks during code execution. Especially useful when doing complex processing of large datasets in the backend.

Installation

$ npm install --save performance-stopwatch

or

$ yarn add performance-stopwatch

Simple Usage

const { Stopwatch } = require('performance-stopwatch');
const sw = new Stopwatch();

sw.start();   // -- (1)
sw.lap();     // -- (2)
sw.lap();     // -- (3)
sw.total();   // -- (4)

This will output:

stopwatch started
100 ms from previous checkpoint
20000 ms from previous checkpoint
20100 ms since start

In the above scenario,

  • 100 ms will be the time taken between (1) and (2)
  • 20000 ms will be the time taken between (2) and (3)
  • 20100 ms will be the time taken since the stopwatch started, which is between (1) and (4)

Advanced Usage

The above setup is straightforward, but has 2 limitations:

  1. There is no way to easily correlate each printed log with the point in code when this happens
  2. It defaults to the use of console.log to print to stdout, which has restrictive logging capabilities

Extra capabilities are presented to deal with the above 2 problems.

Checkpoint messages - Tackling problem (1)

Add a message to each call to any function of stopwatch.

const { Stopwatch } = require('performance-stopwatch');
const sw = new Stopwatch();

sw.start('start of func');
sw.lap('after loop');
sw.lap('after database calls');
sw.total('end of func');

This will output:

start of func - stopwatch started
after loop - 100 ms from previous lap
after database calls - 20000 ms from previous lap
end of func - 20100 ms since start

We can even customize the '-' divider.

const { Stopwatch } = require('performance-stopwatch');
const sw = new Stopwatch({
  divider: '|'
});

sw.start('start of func');
sw.lap('after loop');
sw.lap('after database calls');
sw.total('end of func');

This will output:

start of func | stopwatch started
after loop | 100 ms from previous lap
after database calls | 20000 ms from previous lap
end of func | 20100 ms since start

Custom logger function - Tackling problem (2)

When initialising, simply include a logger-related function that will be called instead of console.log.

const logger = './utils/logger';
const { Stopwatch } = require('performance-stopwatch');
const sw = new Stopwatch({
  loggerFunc: logger.info,
});

// use the stopwatch

Note: we pass in the function that will be called, instead of the logger itself. This allows us to maximise flexibility on your end by being agnostic about how you choose to implement logging.