Property | Type | Description |
---|---|---|
localeOverride? |
string |
Override of the locale used to format and localize the time value. |
unitsMinimum? |
"ns" | "μs" | "ms" | "s" |
Smallest time unit that can be displayed. |
unitsOverride? |
"ns" | "μs" | "ms" | "s" |
Override of time units to display; supersedes unitsMinimum . |
buildServerTimingHeader(
name
,startTime
?,description
?): readonly ["Server-Timing"
,string
]
Build a Server-Timing
header to measure a performance metric using the provided values.
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. |
readonly ["Server-Timing"
, string
]
A Server-Timing
header tuple: ['Server-Timing'
, string
].
import { buildServerTimingHeader } from '@mangs/bun-utils/time';
const startTime = performance.now();
// sometime later...
request.headers.append(...buildServerTimingHeader('metric', startTime, 'It measures everything'));
getElapsedTimeFormatted(
startTime
,formatOptions
?):string
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.
Parameter | Type | Description |
---|---|---|
startTime |
number |
Start time calculated by Bun.nanoseconds() . |
formatOptions ? |
FormatOptions |
Options object for formatting customization. |
string
Localized string showing elapsed time with units.
measureElapsedTime<
T
>(runner
):Promise
<object
>
Measure the execution time of the passed-in function.
Type parameter |
---|
T |
Parameter | Type | Description |
---|---|---|
runner |
() => T | Promise <T > |
Function whose execution duration will be measured. |
Promise
<object
>
Object containing the elapsed time and the return value of the passed-in function.
Member | Type |
---|---|
elapsedTime |
string |
returnValue |
Awaited <T > |
measureServerTiming<
T
>(metricName
,request
,runner
,metricDescription
?):Promise
<T
>
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 parameter |
---|
T |
Parameter | Type | Description |
---|---|---|
metricName |
string |
Name of the Server-Timing metric being measured. |
request |
Request |
Request object to which the Server-Timing header will be appended. |
runner |
() => T | Promise <T > |
Function whose execution duration will be measured. |
metricDescription ? |
string |
Optional description of the Server-Timing metric being measured. |
Promise
<T
>
The return value of the passed-in function.
import { measureServerTiming } from '@mangs/bun-utils/time';
const cmsContent = await measureServerTiming('cmsLoad', request, () =>
getCmsContent('article1'),
);
sleep(
duration
):Promise
<unknown
>
Asynchronous sleep function using promises.
Parameter | Type | Description |
---|---|---|
duration |
number |
Length of time to sleep. |
Promise
<unknown
>
Promise
that resolves when the specified duration expires.