-
-
Notifications
You must be signed in to change notification settings - Fork 33.1k
http: optimize checkIsHttpToken for short strings #59832
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
http: optimize checkIsHttpToken for short strings #59832
Conversation
Review requested:
|
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #59832 +/- ##
==========================================
+ Coverage 88.26% 88.30% +0.03%
==========================================
Files 701 701
Lines 206774 206810 +36
Branches 39778 39780 +2
==========================================
+ Hits 182514 182622 +108
+ Misses 16298 16210 -88
- Partials 7962 7978 +16
🚀 New features to boost your workflow:
|
Use lookup table instead of regex for strings shorter than 10 characters to improve performance for common short header names while maintaining compatibility.
8ce8920
to
b1f8b70
Compare
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.
I like it! Before we merge let's run benchmarks and be sure
Benchmark CI: https://ci.nodejs.org/view/Node.js%20benchmark/job/benchmark-node-micro-benchmarks/1734/console
|
@@ -214,7 +238,19 @@ const tokenRegExp = /^[\^_`a-zA-Z\-0-9!#$%&'*+.|~]+$/; | |||
* @returns {boolean} | |||
*/ | |||
function checkIsHttpToken(val) { | |||
return tokenRegExp.test(val); | |||
if (val.length >= 10) { |
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.
We can probably safely increase this to 30. There are a fair number of common header names that are over 10 characters (e.g. content-length
, authorization
, content-type
, content-security-policy
, etc)
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.
When looking at the benchmark results, it seems that the benefit over a regex is likely zero or negative for more than ten characters.
Landed in c8c6bfa |
Description
Optimize
checkIsHttpToken
performance by using a pre-computed lookup table for strings shorter than 10 characters instead of regexBenchmark Results
Shows significant performance improvements for short strings, with minimal impact on longer strings: