Skip to content

Commit

Permalink
fetch: improve performance of isValidEncodedURL (#3090)
Browse files Browse the repository at this point in the history
  • Loading branch information
Uzlopak committed Apr 11, 2024
1 parent 6481804 commit 21f6477
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
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

0 comments on commit 21f6477

Please sign in to comment.