Skip to content

timeUtils

github-actions edited this page Jun 2, 2026 · 24 revisions

timeUtils

Interfaces

FormatOptions

Defined in: src/timeUtils.mts:11

Properties

Property Type Description Defined in
localeOverride? string Override of the locale used to format and localize the time value. src/timeUtils.mts:15
unitsMinimum? "ns" | "μs" | "ms" | "s" Smallest time unit that can be displayed. src/timeUtils.mts:19
unitsOverride? "ns" | "μs" | "ms" | "s" Override of time units to display; supersedes unitsMinimum. src/timeUtils.mts:23

ServerTimingMetricParsed

Defined in: src/timeUtils.mts:25

Properties

Property Type Defined in
description? string src/timeUtils.mts:26
duration? string src/timeUtils.mts:27
name string src/timeUtils.mts:28

Functions

buildServerTimingHeader()

buildServerTimingHeader(name, startTime?, description?): readonly [readonly ["Server-Timing", string], number]

Defined in: src/timeUtils.mts:47

Build a Server-Timing header to measure a performance metric using the provided values.

Parameters

Parameter Type Description
name string The name of the performance metric.
startTime? number The recorded start time used to compute the metric duration; computed by subtracting the time at which this function is called by the start time. Milliseconds is the unit recommended by the W3C.
description? string A description of the metric.

Returns

readonly [readonly ["Server-Timing", string], number]

A tuple containing a tuple representing the header value and the execution duration.

Example

import { buildServerTimingHeader } from '@mangs/bun-utils/time';

const startTime = performance.now();
// sometime later...
request.headers.append(...buildServerTimingHeader('metric', startTime, 'It measures everything')[0]);

getElapsedTimeFormatted()

getElapsedTimeFormatted(startTime, formatOptions?): string

Defined in: src/timeUtils.mts:62

Get a formatted string representing the time between the provided start time parameter and the time the function is called. An optional options object can be provided to customize formatting.

Parameters

Parameter Type Description
startTime number Start time calculated by Bun.nanoseconds().
formatOptions? FormatOptions Options object for formatting customization.

Returns

string

Localized string showing elapsed time with units.


measureElapsedTime()

measureElapsedTime<TRunner>(runner): Promise<readonly [Awaited<TRunner>, string]>

Defined in: src/timeUtils.mts:102

Measure the execution time of the passed-in function.

Type Parameters

Type Parameter
TRunner

Parameters

Parameter Type Description
runner () => TRunner | Promise<TRunner> Function whose execution duration will be measured.

Returns

Promise<readonly [Awaited<TRunner>, string]>

A tuple containing the return value of the passed-in function and the elapsed execution time.


measureServerTiming()

measureServerTiming<TRunner, TMetricName>(metricName, request, runner, metricDescription?): Promise<readonly [Awaited<TRunner>, number]>

Defined in: src/timeUtils.mts:127

Measure the execution time of the passed-in function, then append to the request object a Server-Timing header containing the specified metric name, the measured duration, and optionally the metric description.

Type Parameters

Type Parameter
TRunner
TMetricName extends string

Parameters

Parameter Type Description
metricName TMetricName Name of the Server-Timing metric being measured.
request Request Request object to which the Server-Timing header will be appended.
runner () => TRunner | Promise<TRunner> Function whose execution duration will be measured.
metricDescription? string Optional description of the Server-Timing metric being measured.

Returns

Promise<readonly [Awaited<TRunner>, number]>

A tuple containing the return value of the passed-in function and the execution duration.

Example

import { measureServerTiming } from '@mangs/bun-utils/time';

const [cmsContent, cmsLoadDuration] = await measureServerTiming('cmsLoad', request, () =>
  getCmsContent('article1'),
);

parseServerTimingMetrics()

parseServerTimingMetrics(serverTimingHeader): ServerTimingMetricParsed[]

Defined in: src/timeUtils.mts:152

Parse Server-Timing header metrics into a simple array of objects wherein each contains up to 3 fields: name, description, and duration.

Parameters

Parameter Type Description
serverTimingHeader string String containing the Server-Timing header value.

Returns

ServerTimingMetricParsed[]

Array of metric objects.

Example

const headerValue = request.headers.get('Server-Timing'); // Contents: metricName;dur=1.23
const metrics = parseServerTimingMetrics(headerValue)
console.log(metrics); // Logs: [{ name: "metricName", description: undefined, duration: 1.23 }]

sleep()

sleep(duration): Promise<unknown>

Defined in: src/timeUtils.mts:168

Asynchronous sleep function using promises.

Parameters

Parameter Type Description
duration number Length of time to sleep.

Returns

Promise<unknown>

Promise that resolves when the specified duration expires.

Clone this wiki locally