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

fetch: improve performance of isValidEncodedURL #3090

Merged
merged 1 commit into from
Apr 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 14 additions & 0 deletions benchmarks/fetch/is-valid-encoded-url.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { bench, run } from 'mitata'
import { isValidEncodedURL } from '../../lib/web/fetch/util.js'

const validUrl = 'https://example.com'
const invalidUrl = 'https://example.com\x00'

bench('isValidEncodedURL valid', () => {
isValidEncodedURL(validUrl)
})
bench('isValidEncodedURL invalid', () => {
isValidEncodedURL(invalidUrl)
})

await run()
16 changes: 8 additions & 8 deletions lib/web/fetch/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,13 @@ function responseLocationURL (response, requestFragment) {
* @returns {boolean}
*/
function isValidEncodedURL (url) {
for (const c of url) {
const code = c.charCodeAt(0)
// Not used in US-ASCII
if (code >= 0x80) {
return false
}
// Control characters
if ((code >= 0x00 && code <= 0x1F) || code === 0x7F) {
for (let i = 0; i < url.length; ++i) {
const code = url.charCodeAt(i)

if (
code > 0x7E || // Non-US-ASCII + DEL
code < 0x20 // Control characters NUL - US
) {
return false
}
}
Expand Down Expand Up @@ -1567,6 +1566,7 @@ function utf8DecodeBytes (buffer) {
module.exports = {
isAborted,
isCancelled,
isValidEncodedURL,
createDeferredPromise,
ReadableStreamFrom,
tryUpgradeRequestToAPotentiallyTrustworthyURL,
Expand Down