header-timers

0.3.0 • Public • Published

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: InstallUsageGoalsServer-Timing Reference

Install

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:

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...

// ...

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...

// 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"

Package Sidebar

Install

npm i header-timers

Weekly Downloads

58

Version

0.3.0

License

Apache-2.0

Unpacked Size

6.88 kB

Total Files

3

Last publish

Collaborators

  • tbeseda