Class PerformanceTracker<T>

A simple tracker to measure the performance of a function/operation.

Please note that this tracker does not handle runtime errors.

async function fetchData() {
return new Promise<string>((resolve) =>
setTimeout(() => resolve('data'), 500),
);
}

const fetchWithTracker = new PerformanceTracker(fetchData, {
name: 'fetchData', // Optional, defaults to function name
onStart: ({ name }) => Logger.debug(`${name} - Operation started`),
onEnd: ({ name, result, duration, metrics }) => {
Logger.debug(`${name} Operation completed in ${duration}ms`, {
result,
metrics,
});
},
});

setInterval(() => {
void fetchWithTracker.run();
}, 1500);

Type Parameters

  • T

Constructors

Properties

Methods

Constructors

  • Type Parameters

    • T

    Parameters

    • fn: () => T | Promise<T>
    • Optionaloptions: { name?: string } & {
          onEnd?: (
              arg: {
                  duration: number;
                  metrics: PerformanceTrackerMetrics;
                  name: string;
                  result: T;
              },
          ) => void;
          onStart?: (arg: { name: string; startTs: number }) => void;
      }

    Returns PerformanceTracker<T>

Properties

options?: { name?: string } & {
    onEnd?: (
        arg: {
            duration: number;
            metrics: PerformanceTrackerMetrics;
            name: string;
            result: T;
        },
    ) => void;
    onStart?: (arg: { name: string; startTs: number }) => void;
}

Methods

  • Type Parameters

    • O extends AnyFunction

    Parameters

    • fn: O = ...

      Any function can be run instead of the one provided in the constructor.

    • Optionalname: string

      Optional name (override) for the operation.

    Returns Promise<ReturnType<O>>