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

statman-stopwatch

v2.18.1

Published

statman-stopwatch is one of the metrics from the statman library. It is a simple high res stopwatch for node.js. Stopwatch is useful for determining the amount of time it takes to perform an activity.

Downloads

32,133

Readme

statman-stopwatch

Node.js CI on npm Known Vulnerabilities Codacy Badge

statman-stopwatch is one of the metrics from the statman library. It is a simple high res stopwatch for node.js. Stopwatch is useful for determining the amount of time it takes to perform an activity.

For example, you may want to determine how long certain potentially expensive activities take in your code (such as calling to an external web services or fetching a dataset from a database). Few lines of code will let you capture that info. There are much more elegant solutions - this is a simple roll-your-own approach.

New Features: {: .label .label-purple }

  • 2.17.1: specify units when reading stopwatch

Install it!

Option 1: access directly (recommended)

Install using npm:

npm install statman-stopwatch

Reference in your app:

const Stopwatch = require('statman-stopwatch');
const stopwatch = new Stopwatch();

Option 2: access from statman (deprecated)

Install using npm:

npm install statman

Reference in your app:

const statman = require('statman');
const stopwatch = new statman.Stopwatch();

Use it!

Constructor

  • Stopwatch() => create instance of a stopwatch
  • Stopwatch(true) => create instance of stopwatch, and have it autostart
  • Stopwatch(name, autostart, delta) => create instance of stopwatch, with name, specify if to autostart, and supply an automatic delta (see setStartTimeDelta)

start

  • start() => starts the stopwatch, let the timing begin!

read

  • read(precision, units) => reads the stopwatch to determine how much time has elapsed. Note that the stopwatch continues to run. Returns the time elapsed in milliseconds.
    • If precision is provided, read() will round to the number of decimals places based on precision.
    • By default, read returns in ms. If units is specified to s, will return values in seconds.
  • time(precision) => alias for read()

stop, suspend

  • stop() => stops the stopwatch, and returns the time elapsed in milliseconds

restart

  • restart() => performs a stop() and start()

split

  • split() => temp stops the stopwatch, allow read() to return time based on when split occurs. Use unsplit() to resume the stopwatch

unsplit

  • unsplit() => use follow a split() to resume the stopwatch

splitTime

  • splitTime => while the stopwatch is split, returns the time as of the split

reset

  • reset() => restores the stopwatch back to init state and clears start and stop times

setStartTimeDelta

  • setStartTimeDelta(number) => provide an elapsed time (in milliseconds) at which to start the stopwatch

resume

  • resume => used in conjunction with suspend to pause/restart the stopwatch

Example

There are some examples in example/example.js

Basic usage

Create a new stopwatch, start() it, and later read() it

    const Stopwatch = require('statman-stopwatch');
    const sw = new Stopwatch();
    sw.start();

    // do some activity

    const delta = sw.read();

Autostart

start() is too hard. Create a new stopwatch with autostart=true, and later read() it

    const Stopwatch = require('statman-stopwatch');
    const sw = new Stopwatch(true);

    // do some activity

    const delta = sw.read();

Stop

Create a new stopwatch, stop() it, and later read() it

    const Stopwatch = require('statman-stopwatch');
    const sw = new Stopwatch(true);

    // do some activity

    sw.stop();

    // do some more activity

    //returns time associated with when stop() occurred
    const delta = sw.read();

Delta

There may be scenarios in which you need to add multiple timings together. To help with this, you can initialize the stopwatch with a value that will be added to the readings. Note that most scenarios could also be achieved by suspending/resuming the stopwatch. Create a new stopwatch, start() it, and later read() it

    const Stopwatch = require('statman-stopwatch');
    const sw = new Stopwatch();
    sw.setStartTimeDelta(5000);
    sw.start();

    // do some activity which takes 500

    const delta = sw.read();
    // delta will be 5500 (the initial 5000ms set in setStartTimeDelta plus the elapsed 500ms)

Suspend/Resume

There are times where you may want to exclude certain events from the stopwatch, so you can suspend (pause) the stopwatch, then resume after the excluded event is complete. Create a new stopwatch, start() it, and later read() it

    const Stopwatch = require('statman-stopwatch');
    const sw = new Stopwatch();
    sw.start();

    // do some activity


    sw.suspend();
    //do some activity that should not be included in the timings
    sw.resume();

    let delta = sw.stop();

Build it!

  • Make sure that you have node and npm installed
  • Clone source code to you local machine
  • Setup dependencies: npm install
  • run tests: npm test