-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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(web): ~400x faster http header trimming #12277
Conversation
Use a regex substring match vs 2 replaces. Roughly ~1.65x faster (423ms vs 255ms in profiled runs)
Check first and and last chars before running regex so already trimmed values are fast, this reduces httpTrims cost from ~255ms => 0.7ms in profiled runs
LGTM. Only possible improvement I could see is to check do the substring manually as well. Something like this: const needsTrimStart = isHttpWhitespace(str[0]);
const needsTrimEnd = isHttpWhitespace(str[str.length - 1]);
if (!needsTrimStart && !needsTrimEnd) return str;
let startIndex = Number(needsTrimStart);
if (needsTrimStart) {
startIndex = getFirstNonHttpWhitespaceIndex(startIndex);
}
let endIndex = needsTrimEnd ? getLastNonHttpWhitespaceIndex(str.length - 2) : undefined;
return str.substring(startIndex, endIndex); |
Had considered that too, but I'm happy mainly optimizing for the fast-path of no trimming right now since that should be the the dominant path too |
Makes sense. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
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)
"", | ||
); | ||
return potentialValue; | ||
return httpTrim(potentialValue); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi! Sorry about my ignorance, but why not just move the entire content of httpTrim
to normalizeHeaderValue
function? That'd remove httpTrim
definition everywhere which is a lot of lines less with the same result
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)