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: avoid Response initialization #2489

Merged
merged 1 commit into from
Dec 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions lib/fetch/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ const {
requestBodyHeader,
subresourceSet
} = require('./constants')
const { kHeadersList } = require('../core/symbols')
const { kHeadersList, kConstruct } = require('../core/symbols')
const EE = require('events')
const { Readable, pipeline } = require('stream')
const { addAbortListener, isErrored, isReadable, nodeMajor, nodeMinor } = require('../core/util')
Expand Down Expand Up @@ -231,9 +231,10 @@ function fetch (input, init = {}) {

// 4. Set responseObject to the result of creating a Response object,
// given response, "immutable", and relevantRealm.
responseObject = new Response()
responseObject = new Response(kConstruct)
responseObject[kState] = response
responseObject[kRealm] = relevantRealm
responseObject[kHeaders] = new Headers(kConstruct)
Comment on lines -234 to +237
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this will improve the performance of the fetch, maybe only a little.

responseObject[kHeaders][kHeadersList] = response.headersList
responseObject[kHeaders][kGuard] = 'immutable'
responseObject[kHeaders][kRealm] = relevantRealm
Expand Down
20 changes: 16 additions & 4 deletions lib/fetch/response.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,10 @@ class Response {
// The static error() method steps are to return the result of creating a
// Response object, given a new network error, "immutable", and this’s
// relevant Realm.
const responseObject = new Response()
const responseObject = new Response(kConstruct)
responseObject[kState] = makeNetworkError()
responseObject[kRealm] = relevantRealm
responseObject[kHeaders] = new Headers(kConstruct)
responseObject[kHeaders][kHeadersList] = responseObject[kState].headersList
responseObject[kHeaders][kGuard] = 'immutable'
responseObject[kHeaders][kRealm] = relevantRealm
Expand All @@ -66,8 +67,11 @@ class Response {
// 3. Let responseObject be the result of creating a Response object, given a new response,
// "response", and this’s relevant Realm.
const relevantRealm = { settingsObject: {} }
const responseObject = new Response()
const responseObject = new Response(kConstruct)
responseObject[kState] = makeResponse({})
responseObject[kRealm] = relevantRealm
responseObject[kHeaders] = new Headers(kConstruct)
responseObject[kHeaders][kHeadersList] = responseObject[kState].headersList
responseObject[kHeaders][kGuard] = 'response'
responseObject[kHeaders][kRealm] = relevantRealm

Expand Down Expand Up @@ -107,8 +111,11 @@ class Response {

// 4. Let responseObject be the result of creating a Response object,
// given a new response, "immutable", and this’s relevant Realm.
const responseObject = new Response()
const responseObject = new Response(kConstruct)
responseObject[kState] = makeResponse({})
responseObject[kRealm] = relevantRealm
responseObject[kHeaders] = new Headers(kConstruct)
responseObject[kHeaders][kHeadersList] = responseObject[kState].headersList
responseObject[kHeaders][kGuard] = 'immutable'
responseObject[kHeaders][kRealm] = relevantRealm

Expand All @@ -127,6 +134,10 @@ class Response {

// https://fetch.spec.whatwg.org/#dom-response
constructor (body = null, init = {}) {
if (body === kConstruct) {
return
}

if (body !== null) {
body = webidl.converters.BodyInit(body)
}
Expand Down Expand Up @@ -258,9 +269,10 @@ class Response {

// 3. Return the result of creating a Response object, given
// clonedResponse, this’s headers’s guard, and this’s relevant Realm.
const clonedResponseObject = new Response()
const clonedResponseObject = new Response(kConstruct)
clonedResponseObject[kState] = clonedResponse
clonedResponseObject[kRealm] = this[kRealm]
clonedResponseObject[kHeaders] = new Headers(kConstruct)
clonedResponseObject[kHeaders][kHeadersList] = clonedResponse.headersList
clonedResponseObject[kHeaders][kGuard] = this[kHeaders][kGuard]
clonedResponseObject[kHeaders][kRealm] = this[kHeaders][kRealm]
Expand Down
Loading