@pipeletteio/time-meter
TypeScript icon, indicating that this package has built-in type declarations

2.0.2 • Public • Published

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

Readme

Keywords

none

Package Sidebar

Install

npm i @pipeletteio/time-meter

Weekly Downloads

3

Version

2.0.2

License

MIT

Unpacked Size

17.5 kB

Total Files

16

Last publish

Collaborators

  • ashed