-
-
Notifications
You must be signed in to change notification settings - Fork 754
perf: optimize check invalid field-vchar #2889
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| import { bench, group, run } from 'mitata' | ||
| import { isValidHeaderChar } from '../../lib/core/util.js' | ||
|
|
||
| const html = 'text/html' | ||
| const json = 'application/json; charset=UTF-8' | ||
|
|
||
| const headerCharRegex = /[^\t\x20-\x7e\x80-\xff]/ | ||
|
|
||
| /** | ||
| * @param {string} characters | ||
| */ | ||
| function charCodeAtApproach (characters) { | ||
| // Validate if characters is a valid field-vchar. | ||
| // field-value = *( field-content / obs-fold ) | ||
| // field-content = field-vchar [ 1*( SP / HTAB ) field-vchar ] | ||
| // field-vchar = VCHAR / obs-text | ||
| for (let i = 0; i < characters.length; ++i) { | ||
| const code = characters.charCodeAt(i) | ||
| // not \x20-\x7e, \t and \x80-\xff | ||
| if ((code < 0x20 && code !== 0x09) || code === 0x7f || code > 0xff) { | ||
| return false | ||
| } | ||
| } | ||
| return true | ||
| } | ||
|
|
||
| group(`isValidHeaderChar# ${html}`, () => { | ||
| bench('regexp.test', () => { | ||
| return !headerCharRegex.test(html) | ||
| }) | ||
| bench('regexp.exec', () => { | ||
| return headerCharRegex.exec(html) === null | ||
| }) | ||
| bench('charCodeAt', () => { | ||
| return charCodeAtApproach(html) | ||
| }) | ||
| bench('isValidHeaderChar', () => { | ||
| return isValidHeaderChar(html) | ||
| }) | ||
| }) | ||
|
|
||
| group(`isValidHeaderChar# ${json}`, () => { | ||
| bench('regexp.test', () => { | ||
| return !headerCharRegex.test(json) | ||
| }) | ||
| bench('regexp.exec', () => { | ||
| return headerCharRegex.exec(json) === null | ||
| }) | ||
| bench('charCodeAt', () => { | ||
| return charCodeAtApproach(json) | ||
| }) | ||
| bench('isValidHeaderChar', () => { | ||
| return isValidHeaderChar(json) | ||
| }) | ||
| }) | ||
|
|
||
| await run() |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -495,6 +495,24 @@ function isValidHTTPToken (characters) { | |
| return true | ||
| } | ||
|
|
||
| // headerCharRegex have been lifted from | ||
| // https://github.com/nodejs/node/blob/main/lib/_http_common.js | ||
|
|
||
| /** | ||
| * Matches if val contains an invalid field-vchar | ||
| * field-value = *( field-content / obs-fold ) | ||
| * field-content = field-vchar [ 1*( SP / HTAB ) field-vchar ] | ||
| * field-vchar = VCHAR / obs-text | ||
| */ | ||
| const headerCharRegex = /[^\t\x20-\x7e\x80-\xff]/ | ||
|
|
||
| /** | ||
| * @param {string} characters | ||
| */ | ||
| function isValidHeaderChar (characters) { | ||
| return !headerCharRegex.test(characters) | ||
| } | ||
|
Comment on lines
+512
to
+514
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. interesting. i wonder how the performance is if we flip it and do it like this const isInvalidHeaderChar = headerCharRegex.test.bind(headerCharRegex)
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is no change.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. thank you for the investigation |
||
|
|
||
| // Parsed accordingly to RFC 9110 | ||
| // https://www.rfc-editor.org/rfc/rfc9110#field.content-range | ||
| function parseRangeHeader (range) { | ||
|
|
@@ -516,7 +534,6 @@ kEnumerableProperty.enumerable = true | |
| module.exports = { | ||
| kEnumerableProperty, | ||
| nop, | ||
|
|
||
| isDisturbed, | ||
| isErrored, | ||
| isReadable, | ||
|
|
@@ -546,6 +563,7 @@ module.exports = { | |
| buildURL, | ||
| addAbortListener, | ||
| isValidHTTPToken, | ||
| isValidHeaderChar, | ||
| isTokenCharCode, | ||
| parseRangeHeader, | ||
| nodeMajor, | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.