perf(http): skip the body stream for a request without a body
CI / dco (pull_request) Successful in 5s
CI / runtimes (bun) (pull_request) Successful in 21s
CI / build (pull_request) Successful in 30s
CI / runtimes (edge) (pull_request) Successful in 24s
CI / runtimes (deno) (pull_request) Successful in 32s

On Node every request goes through readBody, GET included. Subscribing
listeners to an empty request stream measured ~4% slower than the old
async iterator on an 8 KB GET response, in both orders on jarvis.

A request with no Transfer-Encoding and no or a zero Content-Length has
no body (RFC 9112 6.3), so resolve undefined without touching the
stream. Node drains the unread request when the response ends.

Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
This commit is contained in:
chrnx committed 2026-10-26 22:48:15 -05:00
1 parent 704622cf65
commit 88be9b1cb2
2 files changed
+19 -3

No files matched your search

+9 -2
View File
@@ -6,8 +6,15 @@ import { declaresOverLimit } from './body';
export function readBody(req: http.IncomingMessage, maxBodyBytes: number): Promise<Buffer | undefined> {
// A declared length over the limit is refused without waiting for the first chunk, which could
// otherwise sit until the request timeout. The fetch adapter makes the same check.
if (declaresOverLimit(req.headers['content-length'], maxBodyBytes))
return Promise.reject(new HttpError(413, 'Payload Too Large'));
const contentLength = req.headers['content-length'];
if (declaresOverLimit(contentLength, maxBodyBytes)) return Promise.reject(new HttpError(413, 'Payload Too Large'));
// No `Transfer-Encoding` and no (or a zero) `Content-Length` is a request without a body (RFC 9112
// §6.3) — every GET. Node's parser has already finished it, so there is nothing to wait for, and
// subscribing to the stream anyway measured ~4% on an 8 KB GET response. Node drains the unread
// request itself when the response ends.
if (req.headers['transfer-encoding'] === undefined && (contentLength === undefined || contentLength === '0'))
return Promise.resolve(undefined);
// Listeners rather than `for await`: the async iterator costs a promise and a microtask per chunk,
// which a JSON POST pays on every request. The semantics are the iterator's: the running total