/howlongdidittake-js

Measure elapsed time and format the result.

Primary LanguageTypeScriptMIT LicenseMIT

HowLongDidItTake

Measure elapsed time.

  • Simple. Lightweight. TypeScript friendly. Small bundling size.
  • Format the result. Choose s, ms or ns. Even automatically.
  • Limit the precision or the fraction digits.
  • Available on both Node.js and browsers (maybe).

CLI

Use hldit command.

hldit (any command)
hldit (any *.js filepath)
  • On CLI, the unit is determined automatically and the precision is always 2. I.e. just like autoUnit(2) via the API (see below).
  • If a given JavaScript file has a default export of any Promise type, hldit awaits until the Promise is resolved.

API

Import

import * as hldit from "howlongdidittake";

Stopwatch

const getElapsedTime = hldit.stopwatch.autoUnit(2); // give precision
console.log(getElapsedTime());
const getElapsedTime = hldit.stopwatch.withUnit("ms", 2); // give unit & fraction digits
console.log(getElapsedTime());

Measure

const anyAsyncCallback = () => Promise.resolve();

const measure = hldit.measure.autoUnit(2); // give precision
measure(anyAsyncCallback).then((elapsedTime) => console.log(elapsedTime));
const anyAsyncCallback = () => Promise.resolve();

const measure = hldit.measure.withUnit("ms", 2); // give unit & fraction digits
measure(anyAsyncCallback).then((elapsedTime) => console.log(elapsedTime));

hldit.measure has functions for sync callbacks as well.