tiny-tim
raw JSON → 0.0.4 verified Fri May 01 auth: no javascript
A minimal, zero-dependency timer utility for Node.js and the browser. Current stable version 0.0.4 (unreleased release cadence). Tiny-tim provides a simple timer that returns elapsed time in configurable units (ms, s, m, h) with optional suffix. Its key differentiator is extreme small size (156 bytes minified) and simplicity, offering a single function that creates a reusable stopwatch. Compiled with babili for both ES6+ and CommonJS environments. No dependencies, suitable for lightweight instrumentation.
Common errors
error timer is not a function ↓
cause Importing as named import instead of default import in ESM.
fix
Use 'import timer from "tiny-tim"' instead of 'import { timer } from "tiny-tim"'.
error require(...).default is not a function ↓
cause Trying to access .default on CommonJS require result, which already returns the function.
fix
Use 'const timer = require("tiny-tim")' directly without .default.
error Cannot find module 'tiny-tim' ↓
cause Package not installed or missing from node_modules.
fix
Run 'npm install tiny-tim' in your project root.
Warnings
gotcha Timer function returns either a string (with suffix) or a number (without suffix). Developers may expect consistent types. ↓
fix Check the type of the returned value if relying on specific data type.
gotcha The timer is not paused or reset externally; calling the returned function stops the timer and returns the elapsed time. Subsequent calls return the same value (timer dead after first call). ↓
fix Recreate the timer for a new measurement; cannot restart an existing timer.
gotcha Units are single-letter strings. Using full words like 'seconds' will be treated as unrecognized unit, defaulting to milliseconds silently. ↓
fix Use only 'ms', 's', 'm', or 'h'.
gotcha No error thrown for invalid units; timer silently defaults to milliseconds. ↓
fix Validate unit string before passing to timer().
Install
npm install tiny-tim yarn add tiny-tim pnpm add tiny-tim Imports
- default wrong
const timer = require('tiny-tim').defaultcorrectimport timer from 'tiny-tim' - default wrong
import { timer } from 'tiny-tim'correctconst timer = require('tiny-tim')
Quickstart
import timer from 'tiny-tim';
// Create a timer in seconds with suffix
const stopwatch = timer('s', true);
setTimeout(() => {
const elapsed = stopwatch();
console.log(elapsed); // '10s'
}, 10000);
// Reuse the same timer
setTimeout(() => {
console.log(stopwatch()); // '15s'
}, 15000);
// Create timer in milliseconds without suffix
const msTimer = timer();
setTimeout(() => {
console.log(msTimer()); // 1000 (number)
}, 1000);