From def22a942743815954bb6d07aa39efe7b4a0f3cc Mon Sep 17 00:00:00 2001 From: Yagiz Nizipli Date: Tue, 21 Feb 2023 15:04:52 -0500 Subject: [PATCH 1/3] perf: do not call url.format if not needed --- lib/fetch/dataURL.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/lib/fetch/dataURL.js b/lib/fetch/dataURL.js index 0d4a46956db..88ff6791f2b 100644 --- a/lib/fetch/dataURL.js +++ b/lib/fetch/dataURL.js @@ -118,6 +118,9 @@ function dataURLProcessor (dataURL) { * @param {boolean} excludeFragment */ function URLSerializer (url, excludeFragment = false) { + if (!excludeFragment) { + return url.href + } return format(url, { fragment: !excludeFragment }) } From 32f3d8f9bc4a6511f2f7e4d1276ff4dfc469c579 Mon Sep 17 00:00:00 2001 From: Yagiz Nizipli Date: Tue, 21 Feb 2023 15:07:10 -0500 Subject: [PATCH 2/3] perf: return early on parseURL --- lib/core/util.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/lib/core/util.js b/lib/core/util.js index 334bc26c776..75e632118a7 100644 --- a/lib/core/util.js +++ b/lib/core/util.js @@ -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') { From c739f2f08e20724cbdb90a8d9b44cb1e02c1b71c Mon Sep 17 00:00:00 2001 From: Yagiz Nizipli Date: Tue, 21 Feb 2023 15:12:23 -0500 Subject: [PATCH 3/3] perf: remove the usage of url.format from serializer --- lib/fetch/dataURL.js | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/lib/fetch/dataURL.js b/lib/fetch/dataURL.js index 88ff6791f2b..beefad15482 100644 --- a/lib/fetch/dataURL.js +++ b/lib/fetch/dataURL.js @@ -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() @@ -118,10 +117,17 @@ function dataURLProcessor (dataURL) { * @param {boolean} excludeFragment */ function URLSerializer (url, excludeFragment = false) { + const href = url.href + if (!excludeFragment) { - return url.href + return href + } + + const hash = href.lastIndexOf('#') + if (hash === -1) { + return href } - return format(url, { fragment: !excludeFragment }) + return href.slice(0, hash) } // https://infra.spec.whatwg.org/#collect-a-sequence-of-code-points