NextRush
API ReferenceMiddleware

@nextrush/timer

Measure request duration with high-resolution timing and Server-Timing support.

Measure how long requests take to process. Uses performance.now() for sub-millisecond accuracy and supports the Server-Timing header for browser DevTools integration.

Installation

$ pnpm add @nextrush/timer

Minimal Usage

import { createApp } from '@nextrush/core';
import { timer } from '@nextrush/timer';

const app = createApp();
app.use(timer());
// Response header: X-Response-Time: 12.34ms

The duration is stored in ctx.state.responseTime as a number (milliseconds).

API Reference

timer(options?)

Create response time middleware.

function timer(options?: TimerOptions): Middleware;

TimerOptions

PropertyTypeDescription
headerstring= 'X-Response-Time'Response header name
suffixstring= 'ms'Time unit suffix
precisionnumber= 2Decimal places (0–6)
stateKeystring= 'responseTime'Key in ctx.state for duration
exposeHeaderboolean= falseSet response header
now() => number= performance.nowCustom time getter

The exposeHeader option defaults to false. Set it to true to include the timing header in responses.

responseTime(options?)

Alias for timer().

function responseTime(options?: TimerOptions): Middleware;

serverTiming(options?)

Create Server-Timing middleware for browser DevTools integration.

function serverTiming(options?: ServerTimingOptions): Middleware;
import { serverTiming } from '@nextrush/timer';

app.use(serverTiming());
// Header: Server-Timing: total;dur=12.34

ServerTimingOptions

PropertyTypeDescription
metricstring= 'total'Metric name
description?stringOptional metric description
precisionnumber= 2Decimal places (0–6)
stateKeystring= 'responseTime'Key in ctx.state for duration
exposeHeaderboolean= falseSet response header
now() => number= performance.nowCustom time getter

With a description:

app.use(
  serverTiming({
    metric: 'api',
    description: 'API Response Time',
  })
);
// Header: Server-Timing: api;dur=12.34;desc="API Response Time"

detailedTimer(options?)

Create detailed timer middleware that stores a TimingResult object instead of a plain number.

function detailedTimer(options?: DetailedTimerOptions): Middleware;

DetailedTimerOptions (extends TimerOptions)

PropertyTypeDescription
detailedboolean= falseStore TimingResult object in ctx.state

TimingResult

PropertyTypeDescription
durationnumberDuration in milliseconds
formattedstringFormatted string (e.g. "12.34ms")
startnumberStart timestamp from performance.now()
endnumberEnd timestamp from performance.now()

Set detailed: true to get a TimingResult object. Without it, detailedTimer stores a plain number — the same as timer().

import { detailedTimer } from '@nextrush/timer';
import type { TimingResult } from '@nextrush/timer';

app.use(detailedTimer({ detailed: true }));

app.use(async (ctx) => {
  await ctx.next();
  const timing = ctx.state.responseTime as TimingResult;
  console.log(timing.duration); // 12.34
  console.log(timing.formatted); // "12.34ms"
});

Practical Example

Track both database and total timing with Server-Timing headers:

import { serverTiming } from '@nextrush/timer';

// Database timing (custom)
app.use(async (ctx) => {
  const dbStart = performance.now();
  await db.query('SELECT * FROM users');
  const dbTime = performance.now() - dbStart;
  ctx.set('Server-Timing', `db;dur=${dbTime.toFixed(2)};desc="Database"`);
  await ctx.next();
});

// Total request timing
app.use(serverTiming({ metric: 'total' }));

View results in Chrome DevTools → Network → Timing tab.

On this page