-
Notifications
You must be signed in to change notification settings - Fork 549
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
perf: optimize check invalid field-vchar (#2889)
* perf: optimize check invalid field-vchar * add benchmark, use named import, fix regression * fix benchmark * apply suggestions from code review
- Loading branch information
Showing
3 changed files
with
104 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters