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

perf(request): optimize if headers are given #2454

Merged
merged 14 commits into from
Nov 26, 2023
22 changes: 12 additions & 10 deletions lib/fetch/request.js
Expand Up @@ -184,8 +184,10 @@ class Request {
urlList: [...request.urlList]
})

const initHasKey = Object.keys(init).length !== 0

// 13. If init is not empty, then:
if (Object.keys(init).length > 0) {
if (initHasKey) {
// 1. If request’s mode is "navigate", then set it to "same-origin".
if (request.mode === 'navigate') {
request.mode = 'same-origin'
Expand Down Expand Up @@ -416,25 +418,25 @@ class Request {
}

// 32. If init is not empty, then:
if (Object.keys(init).length !== 0) {
if (initHasKey) {
/** @type {HeadersList} */
const headersList = this[kHeaders][kHeadersList]
// 1. Let headers be a copy of this’s headers and its associated header
// list.
let headers = new Headers(this[kHeaders])

// 2. If init["headers"] exists, then set headers to init["headers"].
if (init.headers !== undefined) {
headers = init.headers
}
const headers = init.headers !== undefined ? init.headers : new HeadersList(headersList)

// 3. Empty this’s headers’s header list.
this[kHeaders][kHeadersList].clear()
headersList.clear()
ronag marked this conversation as resolved.
Show resolved Hide resolved

// 4. If headers is a Headers object, then for each header in its header
// list, append header’s name/header’s value to this’s headers.
if (headers.constructor.name === 'Headers') {
if (headers instanceof HeadersList) {
tsctx marked this conversation as resolved.
Show resolved Hide resolved
for (const [key, val] of headers) {
this[kHeaders].append(key, val)
headersList.append(key, val)
}
// Note: Copy the `set-cookie` meta-data.
headersList.cookies = headers.cookies
} else {
// 5. Otherwise, fill this’s headers with headers.
fillHeaders(this[kHeaders], headers)
Expand Down