perf(fetch): avoid duplicate makeRequest and defer Request signal linking #6260
All Vouch review confidence checks passed
Residual risk 0.24 = Change Risk (0.42) × (1 − Review Confidence (0.43)).
Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.42 · 320 lines diff across 4 files
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.02 · 1 substantive comment(s)
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size
diff_size
0.345
Shallow Review Depth
shallow_review_depth
0.344
Real PR Conversation Timeline
1 comments
"Review active on #6260"
general
weight: 0.35
jasnell
Background
While profiling Node.js HTTP servers built on the Web Fetch API (such as
@remix-run/node-fetch-server, which constructs a nativenew Request(url, { method, headers, signal, body })for every incoming HTTP request), two hot-path allocations stood out inRequestconstruction:1.
createDependentSignal()runs eagerly in theRequestconstructor even whenrequest.signalis never read, allocating a secondAbortController,WeakRef,WeakMapentries,FinalizationRegistryregistration, andaddAbortListener/removeEventListenerchurn on GC.2. When
inputis a string, theRequestconstructor callsmakeRequest()twice — allocating and immediately discarding a 40-property internal request object, aHeadersList, aMap, and aurlListarray on every call.Both optimizations preserve Undici's HTTP client behavior and also speed up client
fetch()calls.Summary of changes
AbortSignalinRequest(lib/web/fetch/request.js,lib/web/fetch/index.js):- Store
#sourceSignalin theRequestconstructor and defercreateDependentSignal()untilrequest.signalis accessed via a private#getSignal()helper (avoiding public getter interception when copying or cloning).- If the source signal aborts before
request.signalis read, accessingrequest.signalstill materializes an already-aborted signal with the originalreason.- In
fetch(), checkhasRequestSignal(requestObject)before registering abort listeners so signal-less clientfetch(url)calls skipAbortControllerandaddAbortListenersetup as well.makeRequest()andHeadersListallocations inRequest(lib/web/fetch/request.js):- Construct the internal request record once for string inputs instead of calling
makeRequest()twice.- For
Requestinputs, skip cloninginput.#state.headersListwheninit.headersreplaces it immediately in step 32, and skip materializinginput.#getSignal()wheninit.signaloverrides it.Benchmarks
Measured on Apple M5 Pro, Node.js v24.18.0 (150k iterations per benchmark, with
global.gc()between runs):| Benchmark |
main| This PR | Speedup || --- | ---: | ---: | ---: |
|
new Request(url)|0.301 µs|0.207 µs| 1.45x (-31.2%) ||
new Request(url, { method, headers })|0.848 µs|0.691 µs| 1.23x (-18.5%) ||
new Request(url, { method, headers, signal })|2.227 µs|0.717 µs| 3.11x (-67.8%) |