Skip to content

Commit

Permalink
Performance improvement
Browse files Browse the repository at this point in the history
Skip data parsing if charset is already given in the content-type header

Fixes #305
  • Loading branch information
Richienb committed Oct 2, 2024
1 parent 0fb2cbf commit 6022b84
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion source/utils/get-charset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,15 @@ export default function getCharset(content: Buffer, headers?: Headers) {
charset = parseContentType(contentType);
}

if (charset) {
return charset;
}

// No charset in content type, peek at response body for at most 1024 bytes
const data = content.slice(0, 1024).toString();

// HTML5, HTML4 and XML
if (!charset && data) {
if (data) {
const $ = load(data);

charset = parseContentType(
Expand Down

0 comments on commit 6022b84

Please sign in to comment.