perf(web): ~400x faster http header trimming (#12277)

Use a regex substring match with a first/last char fastpath instead of 2 regex replaces. Roughly ~400x faster (423ms vs 0.7ms in profiled runs)
This commit is contained in:
Aaron O'Mullan 2021-09-30 18:39:55 +02:00 committed by GitHub
parent ee2e25fba7
commit 68e5cdaff0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 36 additions and 14 deletions

View file

@ -15,12 +15,11 @@
const {
HTTP_TAB_OR_SPACE_PREFIX_RE,
HTTP_TAB_OR_SPACE_SUFFIX_RE,
HTTP_WHITESPACE_PREFIX_RE,
HTTP_WHITESPACE_SUFFIX_RE,
HTTP_TOKEN_CODE_POINT_RE,
byteLowerCase,
collectSequenceOfCodepoints,
collectHttpQuotedString,
httpTrim,
} = window.__bootstrap.infra;
const {
ArrayIsArray,
@ -59,17 +58,7 @@
* @returns {string}
*/
function normalizeHeaderValue(potentialValue) {
potentialValue = StringPrototypeReplaceAll(
potentialValue,
HTTP_WHITESPACE_PREFIX_RE,
"",
);
potentialValue = StringPrototypeReplaceAll(
potentialValue,
HTTP_WHITESPACE_SUFFIX_RE,
"",
);
return potentialValue;
return httpTrim(potentialValue);
}
/**
@ -95,7 +84,7 @@
// Regex matching illegal chars in a header value
// deno-lint-ignore no-control-regex
const ILLEGAL_VALUE_CHARS = /[\x00\x0A\x0D]/;
const ILLEGAL_VALUE_CHARS = /[\x00\x0A\x0D]/g;
/**
* https://fetch.spec.whatwg.org/#concept-headers-append