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

ts-stopwatch

v0.0.4

Published

Stopwatch timer utility written in TypeScript

Downloads

23,929

Readme

npm version Join the chat at https://gitter.im/ts-stopwatch/Lobby Build Status Coverage Status

ts-stopwatch

Stopwatch timer utility written in TypeScript

Contents

What is it?

ts-stopwatch is a convenient stopwatch-inspired timer utility for measuring durations of time between different points of execution in code. It can be paused, resumed, reset, and can record multiple "slices" of time (inspired by a stopwatch's "lap" functionality).

Because ts-stopwatch is written in TypeScript, it comes with 100% accurate TypeScript type definitions.

Is TypeScript required?

No. The ts-stopwatch NPM package includes both a standard NPM/CommonJS module and a ES module. The included TypeScript type definitions are just a bonus. Source maps are also included.

Installation

Install via NPM:

npm i -s ts-stopwatch

Import the Stopwatch class

JavaScript:

const Stopwatch = require("ts-stopwatch").Stopwatch;

TypeScript/ES6:

import { Stopwatch } from "ts-stopwatch";

How to use it

To begin recording time, create a new instance of Stopwatch and call its start() method.

Pause the stopwatch via stop(), then resume by calling start() again.

Use getTime() to get the amount of time that the stopwatch has recorded so far (ignoring durations of time that the Stopwatch was stopped). There's no need to stop the stopwatch before doing this.

Similar to advanced physical stopwatches' abilities to record multiple lap times Stopwatch supports recording multiple "slices" of time. See slice(), getPendingSlice(), getCompletedSlices(), and getCompletedAndPendingSlices().

NOTE: Call stop(true) to simultaneously record the current pending "slice".

Use reset() to reset the stopwatch to its initial state.

NOTE: Call start(true) to force a reset before (re)starting.

See getState(), isIdle(), isRunning(), and isStopped() for testing the current state of the Stopwatch.

Customize the source of time

By default, Stopwatch internally uses Date.now() for tracking the amount of time that has passed. This is the most compatible implementation, but has some limitations:

  • Maximum potential precision of 1ms.
  • Results can be thrown off if the computer's time is adjusted (manually or automatically) during execution of the code.

If your runtime environment supports a more reliable or higher precision method for obtaining system time or program execution time, then you can override this default implementation by either:

  • Providing a custom "system time getter" function to the Stopwatch constructor.
  • Or using Stopwatch.setDefaultSystemTimeGetter() to ensure that ALL future instances of Stopwatch use your custom "system time getter" by default.

NOTE: The unit of time/duration reported by Stopwatch is determined by the unit time returned by the "system time getter" function.

Stopwatch is not limited to recording durations of system time. It can record the "duration" (change) of any numeric value that may change over time, but is guaranteed to never decrease over time.

Usage Examples

Basic Usage

import { Stopwatch } from "ts-stopwatch";

const stopwatch = new Stopwatch();

stopwatch.start();
// imagine 100 ms worth of code execution
stopwatch.stop();
// imagine 100 ms worth of code execution (ignored)
stopwatch.start();
// imagine 100 ms worth of code execution
stopwatch.stop();
// imagine 100 ms worth of code execution (ignored)

stopwatch.getTime();
// returns 200
// (amount of time the stopwatch has been running)

Time Slices

import { Stopwatch } from "ts-stopwatch";

const stopwatch = new Stopwatch();

stopwatch.start();
// imagine 100 ms worth of code execution
stopwatch.slice();
// imagine 50 ms worth of code execution
stopwatch.stop();
// imagine 100 ms worth of code execution (ignored)
stopwatch.slice();
// imagine 100 ms worth of code execution (ignored)
stopwatch.start();
// imagine 150 ms worth of code execution
stopwatch.slice();

stopwatch.getCompletedSlices();
// returns [
//   {
//     startTime: 0,
//     stopTime: 100,
//     duration: 100
//   },
//   {
//     startTime: 100,
//     stopTime: 150,
//     duration: 50
//   },
//   {
//     startTime: 150,
//     stopTime: 300,
//     duration: 150
//   }
// ]

API Reference

Under construction.

View source code for exact method signatures and complete method/parameter documentation.

The distributed code and type definitions also include full documentation for convenience in IDEs that display documentation.