Registry / observability / ts-stopwatch

ts-stopwatch

JSON →
library0.0.4jsnpmunverified

ts-stopwatch is a convenient, stopwatch-inspired timer utility for measuring durations of time between different points of execution in code. It supports pausing, resuming, resetting, and recording multiple 'slices' of time, akin to a physical stopwatch's lap functionality. Written entirely in TypeScript, it provides 100% accurate type definitions, making it suitable for both TypeScript and JavaScript environments. The package, currently at version 0.0.4, appears to have an inactive development status, with no significant updates in recent years. A key feature is its ability to customize the underlying time source, allowing developers to opt for more precise or stable timers like `process.hrtime()` in Node.js, addressing potential inaccuracies from system clock adjustments that `Date.now()` might face. It ships with both CommonJS and ES modules.

npm install ts-stopwatch
INSTALL
IMPORT
SIG · TS-STOPWATCH
T
ts-stopwatch
observabilityjavascriptv0.0.4
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

Stopwatch
import { Stopwatch } from 'ts-stopwatch';
import Stopwatch from 'ts-stopwatch';
The primary `Stopwatch` class is a named export.
Stopwatch (CommonJS)
const { Stopwatch } = require('ts-stopwatch');
const Stopwatch = require('ts-stopwatch');
CommonJS users must destructure the named export from the module object.
Type definitions
import { Stopwatch } from 'ts-stopwatch';
TypeScript types are included automatically; no separate type imports are generally needed for the main class.

Demonstrates basic stopwatch functionality: starting, stopping, getting time, pausing/resuming, and recording time slices (laps).

import { Stopwatch } from 'ts-stopwatch'; // Basic usage console.log('--- Basic Usage ---'); const stopwatch = new Stopwatch(); stopwatch.start(); console.log('Stopwatch started...'); setTimeout(() => { stopwatch.stop(); console.log(`Stopwatch stopped at: ${stopwatch.getTime()}ms`); setTimeout(() => { stopwatch.start(); console.log('Stopwatch resumed...'); setTimeout(() => { stopwatch.stop(); console.log(`Stopwatch stopped again at: ${stopwatch.getTime()}ms`); console.log('--- Time Slices ---'); // Time slices (laps) const lapStopwatch = new Stopwatch(); lapStopwatch.start(); console.log('Lap stopwatch started...'); lapStopwatch.slice(); // Records the start of the first slice setTimeout(() => { lapStopwatch.slice(); // Records the end of the first, start of second console.log(`Completed slices: ${JSON.stringify(lapStopwatch.getCompletedSlices())}`); setTimeout(() => { lapStopwatch.stop(true); // Stop and record pending slice console.log(`All slices: ${JSON.stringify(lapStopwatch.getCompletedAndPendingSlices())}`); lapStopwatch.reset(); console.log('Lap stopwatch reset.'); }, 50); }, 50); }, 50); }, 50); }, 50); // Example with custom time getter (Node.js high-resolution time) // Note: process.hrtime() returns [seconds, nanoseconds], need conversion // if (typeof process !== 'undefined' && process.hrtime) { // const hrStopwatch = new Stopwatch(() => { // const hrTime = process.hrtime(); // return hrTime[0] * 1000 + hrTime[1] / 1000000; // }); // hrStopwatch.start(); // setTimeout(() => { // hrStopwatch.stop(); // console.log(`High-res stopwatch time: ${hrStopwatch.getTime()}ms`); // }, 10); // }
Debug
Known issues
abandonedThe `ts-stopwatch` package appears to be abandoned. The last version (0.0.4) was published over 7 years ago, and there has been no recent activity on its GitHub repository. This means it may contain unaddressed bugs, security vulnerabilities, or lack compatibility with newer TypeScript/Node.js versions.
fix
Consider using a more actively maintained alternative for critical projects. If using, thoroughly vet the code and consider vendoring it into your project for local maintenance.
affects: >=0.0.4
gotchaBy default, `ts-stopwatch` uses `Date.now()` for time tracking. This method has a maximum precision of 1ms and can be inaccurate if the system clock is adjusted during execution, or for very short durations.
fix
For higher precision or robustness against system clock changes (especially in Node.js environments), provide a custom 'system time getter' function to the `Stopwatch` constructor, for example, by integrating `process.hrtime()`.
affects: >=0.0.1
gotchaWhen using time slices (laps), calling `stop()` without the `true` argument will stop the stopwatch but *not* record the currently pending slice. The `getPendingSlice()` method will still reflect the unrecorded duration.
fix
To stop the stopwatch and simultaneously record the final pending slice, call `stop(true)`.
affects: >=0.0.1
Errors
Common errors & fixes
TypeError: Cannot read properties of undefined (reading 'Stopwatch') OR TypeError: Stopwatch is not a constructor
Attempting to use `require('ts-stopwatch')` directly as a constructor in CommonJS, rather than destructuring the named export.
fix
In CommonJS, you must destructure the `Stopwatch` class: `const { Stopwatch } = require('ts-stopwatch');`
ReferenceError: Stopwatch is not defined
The `Stopwatch` class was not correctly imported or is out of scope.
fix
Ensure you have `import { Stopwatch } from 'ts-stopwatch';` at the top of your ES Module/TypeScript file, or `const { Stopwatch } = require('ts-stopwatch');` in your CommonJS file, and that the variable is accessible in the current scope.
Upgrade
Version history
0.0.4latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
19 hits · last 30 days
node
16
OpenAI (training)
1
Resources
ts-stopwatch — npm install ts-stopwatch · libregistry