The node.js team has an alternative HTTP client, it can run HTTP requests about 2-3 times faster than the built-in HTTP module. With node.js version 15, the advantage is up to 4 times. This is important because almost every other HTTP client such as node-fetch
, request
, or axios
use the original build-in HTTP client of node.js under the hood. And after the request
lib is marked deprecated, here might be a good alternative.
The new HTTP client is called undici and can be found on npm and on GitHub under the node.js organization.
What is the difference?
- It requires to create a
client
object for every destination server you want to access. Still, the API is clean and you will be quickly able to adopt it. In the constructor, you have some options for concurrency. - The client uses the build-in
net
module instead of thehttp
module. - As the HTTP lib, it uses
keepAlive
to use multiple queries over the same TCP connection, but it does not wait for responses to send out more requests. In the documentation they name it pipelining.
Afterthoughts
Do you think fastify also became faster than the native http module by using the net
module? I have to read that code.
Earlier this year I build an experimental web server using the net
module. you can read my article.
What are your thoughts on undici
? Please leave a nice comment.
Top comments (1)
could this be used to make http long polling more efficient? still the most reliable way to do websockets through firewalls, for chatty apps... it supports streaming so...
client.stream(opts, factory(data)[, callback(err)]): Promise|Void