Skip to content

time

time: <A>(fn, config) => (a) => A

Defined in: Composition/tap.ts:204

Runs a function and measures its execution duration, returning the value unchanged. Supports both synchronous and asynchronous functions. If the timed function returns a Promise, duration measurement resolves asynchronously upon resolution/rejection.

A

(a) => unknown

TimeConfig

(a) => A

const data = [1, 2, 3];
const processData = (d: typeof data) => d.map(n => n * 2);
const fetchData = async (d: typeof data) => d.length;
const metrics = { histogram: (name: string, ms: number) => {} };

// Time a synchronous computation
pipe(
  data,
  tap.time(processData, { label: "sync-process" })
);

// Time an asynchronous fetch with custom metrics callback
pipe(
  data,
  tap.time(fetchData, {
    onFinish: (dur) => metrics.histogram("api.time", Duration.to.milliseconds(dur))
  })
);