perf(node): parse a repeated content-type charset once

Clients send the same Content-Type with every request, so the charset of the last header parsed is kept instead of parsing the header again. Media types without parameters still return before it.

Co-authored-by: Roman Khegay <rk@garuna.dev>
This commit is contained in:
Kamil MyśliwiecandRoman Khegay committed 2026-10-14 10:50:47 +02:00
1 parent b2cb76a187
commit 13619b662e
1 file changed
+17 -7
@@ -200,19 +200,29 @@ function getMediaType(req: IncomingMessage): string {
.toLowerCase();
}
// A client sends the same header with every request, so it is parsed once
let cachedHeader = '';
let cachedCharset: string | undefined;
function getCharset(req: IncomingMessage): string | undefined {
const header = req.headers['content-type'];
if (!header || header.indexOf(';') === -1) {
return undefined;
}
try {
// RFC 9110 parameter parsing (quoted strings included), as body-parser
// does, so that a "charset=" inside another parameter's quoted value is
// not mistaken for the charset
return contentType.parse(header).parameters.charset?.toLowerCase();
} catch {
return undefined;
if (header !== cachedHeader) {
cachedHeader = header;
try {
// RFC 9110 parameter parsing (quoted strings included), as body-parser
// does, so that a "charset=" inside another parameter's quoted value is
// not mistaken for the charset
cachedCharset = contentType
.parse(header)
.parameters.charset?.toLowerCase();
} catch {
cachedCharset = undefined;
}
}
return cachedCharset;
}
function createTypeMatcher(