Skip to content

Commit

Permalink
perf(request): optimize if headers are given (#2454)
Browse files Browse the repository at this point in the history
* perf(request): optimize if headers are given

* perf: use shallow copy

* fix

* spec

* make maintenance easier

* fix: comment position

* fix

* short

* fix

* add comment

* use append

* fix: typo

* refactor
  • Loading branch information
tsctx committed Nov 26, 2023
1 parent ac30400 commit 0c45ff7
Showing 1 changed file with 12 additions and 10 deletions.
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()

// 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) {
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

0 comments on commit 0c45ff7

Please sign in to comment.