An open API service indexing awesome lists of open source software.

https://github.com/tbeseda/header-timers

Server-Timing HTTP header helper
https://github.com/tbeseda/header-timers

headers http nodejs server-timing timers

Last synced: 10 months ago
JSON representation

Server-Timing HTTP header helper

Awesome Lists containing this project

README

          


header-timers ⏱️


Server-Timing HTTP header helper

Create timers and report to the browser with a Server-Timing header.

header-timers on npmjs.org »



Contents:
Install

Usage

Goals

Server-Timing Reference

## Install

```sh
npm i header-timers
```

> [!NOTE]
> This is a Node.js library and won't work in a browser runtime.

## Usage

Just .js samples for now:

```js
import HeaderTimers from 'header-timers'

const timers = HeaderTimers()
const { start, stop } = timers

start('open')
stop('open')

start('database', 'collecting data') // with a description

start('analytics', 'log request data')
await new Promise(resolve => setTimeout(resolve, 100))
stop('analytics')

await new Promise(resolve => setTimeout(resolve, 500))
stop('database')

console.log('TIMER COUNT', timers.timers().length) // 3

console.log(timers.key)
// "Server-Timing" - just in case you need it!
console.log(timers.timers())
// [ Array of Timer Objects ]
console.log(timers.values())
/**
[
'open;dur=0.026ms',
'database;desc="collecting data";dur=603.098792ms',
'analytics;desc="log request data";dur=101.776917ms'
]
*/
console.log(timers.value())
// "open;dur=0.014ms, database;desc="collecting data";dur=603.512ms, analytics;desc="log request data";dur=101.475709ms"
console.log(timers.string())
// "Server-Timing: open;dur=0.014ms, database;desc="collecting data";dur=603.512ms, analytics;desc="log request data";dur=101.475709ms"
console.log(timers.object())
/**
{
'Server-Timing': 'open;dur=0.026ms, database;desc="collecting data";dur=603.098792ms, analytics;desc="log request data";dur=101.776917ms'
}
*/
```

Also...

```js
// ...

start("this won't work")
timers.reset()
stop("this won't work")

console.log('TIMER COUNT', timers.timers().length) // 0

start('reset')
await new Promise(resolve => setTimeout(resolve, 100))
stop('reset')

console.log('TIMER COUNT', timers.timers().length) // 1

console.log('NEW Header String', timers.string())
// NEW Header String "Server-Timing: reset;dur=101.799166ms"
```

And...

```js
// names optional; be aware of the order, though!
start() // 1
stop() // stop 1 => <1ms

start() // 2
start('foo')
start() // 3

stop() // stop 3 => <1ms

await new Promise(resolve => setTimeout(resolve, 100))

stop() // stop 2 => 100+ms
stop('foo') // => 100+ms
```

## Goals

Be helpful and accurate. Stay small and fast. Then be intuitive. This means:

- zero dependencies
- measure in nanoseconds
- option to disable
- fast primitives/operations (this needs review)
- no Classes
- terse but readable API

---

_"It's just strings all the way down"_