-
Notifications
You must be signed in to change notification settings - Fork 1
timeUtils
Defined in: src/timeUtils.mts:11
| 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 |
Defined in: src/timeUtils.mts:25
| Property | Type | Defined in |
|---|---|---|
description?
|
string |
src/timeUtils.mts:26 |
duration?
|
string |
src/timeUtils.mts:27 |
name
|
string |
src/timeUtils.mts:28 |
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.
| 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 [readonly ["Server-Timing", string], number]
A tuple containing a tuple representing the header value and the execution duration.
import { buildServerTimingHeader } from '@mangs/bun-utils/time';
const startTime = performance.now();
// sometime later...
request.headers.append(...buildServerTimingHeader('metric', startTime, 'It measures everything')[0]);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.
| 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<
TRunner>(runner):Promise<readonly [Awaited<TRunner>,string]>
Defined in: src/timeUtils.mts:102
Measure the execution time of the passed-in function.
| Type Parameter |
|---|
TRunner |
| Parameter | Type | Description |
|---|---|---|
runner |
() => TRunner | Promise<TRunner> |
Function whose execution duration will be measured. |
Promise<readonly [Awaited<TRunner>, string]>
A tuple containing the return value of the passed-in function and the elapsed execution time.
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 Parameter |
|---|
TRunner |
TMetricName extends string
|
| 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. |
Promise<readonly [Awaited<TRunner>, number]>
A tuple containing the return value of the passed-in function and the execution duration.
import { measureServerTiming } from '@mangs/bun-utils/time';
const [cmsContent, cmsLoadDuration] = await measureServerTiming('cmsLoad', request, () =>
getCmsContent('article1'),
);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.
| Parameter | Type | Description |
|---|---|---|
serverTimingHeader |
string |
String containing the Server-Timing header value. |
Array of metric objects.
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(
duration):Promise<unknown>
Defined in: src/timeUtils.mts:168
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.