Skip to content

Commit

Permalink
perf: optimize happy path (nodejs#1955)
Browse files Browse the repository at this point in the history
* perf: do not call url.format if not needed

* perf: return early on parseURL

* perf: remove the usage of url.format from serializer
  • Loading branch information
anonrig authored and crysmags committed Feb 27, 2024
1 parent 65fd4e7 commit eff5e3e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
6 changes: 6 additions & 0 deletions lib/core/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ function buildURL (url, queryParams) {
function parseURL (url) {
if (typeof url === 'string') {
url = new URL(url)

if (!/^https?:/.test(url.origin || url.protocol)) {
throw new InvalidArgumentError('invalid protocol')
}

return url
}

if (!url || typeof url !== 'object') {
Expand Down
13 changes: 11 additions & 2 deletions lib/fetch/dataURL.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
const assert = require('assert')
const { atob } = require('buffer')
const { format } = require('url')
const { isValidHTTPToken, isomorphicDecode } = require('./util')

const encoder = new TextEncoder()
Expand Down Expand Up @@ -118,7 +117,17 @@ function dataURLProcessor (dataURL) {
* @param {boolean} excludeFragment
*/
function URLSerializer (url, excludeFragment = false) {
return format(url, { fragment: !excludeFragment })
const href = url.href

if (!excludeFragment) {
return href
}

const hash = href.lastIndexOf('#')
if (hash === -1) {
return href
}
return href.slice(0, hash)
}

// https://infra.spec.whatwg.org/#collect-a-sequence-of-code-points
Expand Down

0 comments on commit eff5e3e

Please sign in to comment.