Skip to content

Instantly share code, notes, and snippets.

@krisanalfa
Last active March 27, 2019 08:16
Show Gist options
  • Save krisanalfa/8359401bd5930d63a5385afecdc33d39 to your computer and use it in GitHub Desktop.
Save krisanalfa/8359401bd5930d63a5385afecdc33d39 to your computer and use it in GitHub Desktop.
// @ts-check
const https = require('https')
/**
* @param {[number, number]} hrtime
*
* @returns {string}
*/
const parseHrtimeToSeconds = hrtime => (hrtime[0] + (hrtime[1] / 1e9)).toFixed(5)
const url = process.argv.pop()
console.info('Requesting %s ...', url)
const timings = {
dns: '',
tcp: '',
tls: '',
ttfb: '',
total: ''
}
const start = process.hrtime()
const req = https.get(url, response => {
response.once('readable', () => {
timings.ttfb = parseHrtimeToSeconds(process.hrtime(start))
})
response.on('data', () => {})
response.on('end', () => {
timings.total = parseHrtimeToSeconds(process.hrtime(start))
})
response.on('close', () => console.table(timings))
})
req.on('socket', socket => {
socket.on('lookup', () => {
timings.dns = parseHrtimeToSeconds(process.hrtime(start))
})
socket.on('connect', () => {
timings.tcp = parseHrtimeToSeconds(process.hrtime(start))
})
socket.on('secureConnect', () => {
timings.tls = parseHrtimeToSeconds(process.hrtime(start))
})
})
@krisanalfa
Copy link
Author

Usage:

node first-byte-time.js [URL]

Example:

node first-byte-time.js https://google.com

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment