What is @szmarczak/http-timer?
@szmarczak/http-timer is an npm package designed to measure the timing of HTTP requests. It provides detailed timing information for various phases of an HTTP request, such as DNS lookup, TCP connection, and response time. This can be useful for performance monitoring and debugging.
What are @szmarczak/http-timer's main functionalities?
Measure HTTP request timings
This feature allows you to measure the timings of an HTTP request. The code sample demonstrates how to use the package to measure and log the timings of a GET request to 'http://example.com'.
const http = require('http');
const timer = require('@szmarczak/http-timer');
const request = http.get('http://example.com', response => {
timer(request);
response.on('end', () => {
console.log(request.timings);
});
});
Detailed timing phases
This feature provides detailed timing information for different phases of an HTTP request. The code sample shows how to log the DNS lookup time, TCP connection time, time to first byte, download time, and total time for a GET request.
const http = require('http');
const timer = require('@szmarczak/http-timer');
const request = http.get('http://example.com', response => {
timer(request);
response.on('end', () => {
console.log('DNS Lookup Time:', request.timings.dns);
console.log('TCP Connection Time:', request.timings.tcp);
console.log('First Byte Time:', request.timings.firstByte);
console.log('Download Time:', request.timings.download);
console.log('Total Time:', request.timings.total);
});
});
Other packages similar to @szmarczak/http-timer
axios
Axios is a popular HTTP client for Node.js and the browser. It supports request and response interceptors, automatic transforms for JSON data, and more. While it does not provide detailed timing information out of the box, it can be extended with interceptors to measure request timings.
request
Request is a simplified HTTP client for Node.js. It is known for its ease of use and flexibility. Although it does not natively support detailed timing information, it can be combined with other packages or custom code to achieve similar functionality.
got
Got is a lightweight and powerful HTTP request library for Node.js. It includes built-in support for retries, streams, and hooks. Got also provides detailed timing information similar to @szmarczak/http-timer, making it a strong alternative.