@consumidor-positivo/ts-utils - v1.21.0
    Preparing search index...

    Function timer

    • Creates a simple high‑resolution timer.

      Uses performance.now() when available (falling back to Date.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.

      Parameters

      • unit: TimeUnit = 'ms'

        Time unit for the result ('ms' | 's' | 'm')

      • precise: boolean = true

        Whether to return full precision (default: true). If false, rounds to 2 decimal places.

      Returns { stop: () => number }

      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`);