Time unit for the result ('ms' | 's' | 'm')
Whether to return full precision (default: true). If false, rounds to 2 decimal places.
Object with a stop() method that returns the elapsed time in the chosen unit
import { timer } from '@consumidor-positivo/ts-utils';
// Measure duration in milliseconds (default)
const t = timer();
// ... perform some work
const elapsedMs = t.stop();
console.log(`Elapsed: ${elapsedMs}ms`);
// Measure duration in seconds
const tSec = timer('s');
// ... HTTP call
const elapsedSeconds = tSec.stop();
console.log(`Elapsed: ${elapsedSeconds}s`);
// Measure duration in minutes
const tMin = timer('m');
// ... long process
const elapsedMinutes = tMin.stop();
console.log(`Elapsed: ${elapsedMinutes}m`);
*
* // Round to 2 decimals (non-precise)
* const tRounded = timer('s', false);
* await fetch('/api');
* console.log(`Elapsed (rounded): ${tRounded.stop()}s`);
Creates a simple high‑resolution timer.
Uses
performance.now()when available (falling back toDate.now()), providing suitable precision to measure durations of operations like HTTP calls.Call
stop()to obtain the elapsed time in the desired unit. By default, it returns milliseconds.