Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf(ext/headers): use regex.test instead of .exec #20125

Merged
merged 3 commits into from
Aug 12, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 12 additions & 13 deletions ext/fetch/20_headers.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,7 @@ const {
ArrayPrototypeSplice,
ObjectEntries,
ObjectHasOwn,
RegExpPrototypeExec,
SafeMap,
MapPrototypeGet,
MapPrototypeHas,
MapPrototypeSet,
MapPrototypeClear,
RegExpPrototypeTest,
Symbol,
SymbolFor,
SymbolIterator,
Expand Down Expand Up @@ -101,19 +96,23 @@ function checkForInvalidValueChars(value) {
return true;
}

const HEADER_NAME_CACHE = new SafeMap();
let HEADER_NAME_CACHE = {};
let HEADER_CACHE_SIZE = 0;
const HEADER_NAME_CACHE_SIZE_BOUNDARY = 4096;
function checkHeaderNameForHttpTokenCodePoint(name) {
if (MapPrototypeHas(HEADER_NAME_CACHE, name)) {
return MapPrototypeGet(HEADER_NAME_CACHE, name);
const fromCache = HEADER_NAME_CACHE[name];
if (fromCache !== undefined) {
return fromCache;
}

const valid = RegExpPrototypeExec(HTTP_TOKEN_CODE_POINT_RE, name) !== null;
const valid = RegExpPrototypeTest(HTTP_TOKEN_CODE_POINT_RE, name);

if (HEADER_NAME_CACHE.size > HEADER_NAME_CACHE_SIZE_BOUNDARY) {
MapPrototypeClear(HEADER_NAME_CACHE);
if (HEADER_CACHE_SIZE > HEADER_NAME_CACHE_SIZE_BOUNDARY) {
HEADER_NAME_CACHE = {};
HEADER_CACHE_SIZE = 0;
}
MapPrototypeSet(HEADER_NAME_CACHE, name, valid);
HEADER_CACHE_SIZE++;
HEADER_NAME_CACHE[name] = valid;

return valid;
}
Expand Down