Options
All
  • Public
  • Public/Protected
  • All
Menu

@pipeletteio/time-meter

@pipeletteio/time-meter

🕑 A minimal time-meter to calculate time between anything.

Installation

npm install @pipeletteio/time-meter

Docs:

Read documentation here.

Example:

const { TimeMeter } = require('@pipeletteio/time-meter');

const meter = new TimeMeter();

setTimeout(() => console.log(`${meter.next()}ms`), 200);

With Typescript:

import { TimeMeter } from '@pipeletteio/time-meter';

const meter = new TimeMeter();

setTimeout(() => console.log(`${meter.next()}ms`), 200);

A shared time meter instance can be retreived from the package (default meter uses the milisecond format):

import { meter } from '@pipeletteio/time-meter';

Using a custom formatter (availables: LegacyFormatter, MilisecondFormatter and NanosecondFormatter):

import { TimeMeter, LegacyFormatter } from '@pipeletteio/time-meter';

const meter = new TimeMeter({ formatter: new LegacyFormatter() });

setTimeout(() => {
const [seconds, nanoseconds] = meter.next();
console.log(`${seconds}s ${nanoseconds}ns`);
}, 200);

Using your own formatter:

import { TimeMeter, TimeFormatterInterface } from '@pipeletteio/time-meter';

class SecondFormatter implements TimeFormatterInterface {
static DIVIDER = BigInt(1e9);

format (ns: bigint): number {
return Number(ns / SecondFormatter.DIVIDER);
}
}

const meter = new TimeMeter({ formatter: new SecondFormatter() });

setTimeout(() => console.log(`${meter.next()}s`), 1500);

Generated using TypeDoc