Skip to content

Commit

Permalink
perf: avoid Response initialization (nodejs#2489)
Browse files Browse the repository at this point in the history
  • Loading branch information
tsctx authored and crysmags committed Feb 27, 2024
1 parent eef4a6e commit 1558cd4
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
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)
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

0 comments on commit 1558cd4

Please sign in to comment.