← Back to Pull requests

perf(fetch): avoid duplicate makeRequest and defer Request signal linking #6260

Open
unknown opened this pull request with 320 lines diff across 4 files
View on GitHub
unknown unknown commented on Nov 26, 2026 02:07 UTC
Author

Background

While profiling Node.js HTTP servers built on the Web Fetch API (such as @remix-run/node-fetch-server, which constructs a native new Request(url, { method, headers, signal, body }) for every incoming HTTP request), two hot-path allocations stood out in Request construction:

1. createDependentSignal() runs eagerly in the Request constructor even when request.signal is never read, allocating a second AbortController, WeakRef, WeakMap entries, FinalizationRegistry registration, and addAbortListener / removeEventListener churn on GC.
2. When input is a string, the Request constructor calls makeRequest() twice — allocating and immediately discarding a 40-property internal request object, a HeadersList, a Map, and a urlList array on every call.

Both optimizations preserve Undici's HTTP client behavior and also speed up client fetch() calls.

Summary of changes

  • Lazy dependent AbortSignal in Request (lib/web/fetch/request.js, lib/web/fetch/index.js):

  • - Store #sourceSignal in the Request constructor and defer createDependentSignal() until request.signal is accessed via a private #getSignal() helper (avoiding public getter interception when copying or cloning).
    - If the source signal aborts before request.signal is read, accessing request.signal still materializes an already-aborted signal with the original reason.
    - In fetch(), check hasRequestSignal(requestObject) before registering abort listeners so signal-less client fetch(url) calls skip AbortController and addAbortListener setup as well.
  • Avoid duplicate makeRequest() and HeadersList allocations in Request (lib/web/fetch/request.js):

  • - Construct the internal request record once for string inputs instead of calling makeRequest() twice.
    - For Request inputs, skip cloning input.#state.headersList when init.headers replaces it immediately in step 32, and skip materializing input.#getSignal() when init.signal overrides 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%) |

    Diff: 320 lines (+240 / -80) across 4 files Reviewer: jasnell Review duration: 7008m
    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
    0.42
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.02 · 1 substantive comment(s)
    0.02
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    42%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    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
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 43.0% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 41.9% (MEDIUM) · Higher = larger blast radius
    41.9%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 43.0% · Lower = rushed or shallow review
    43.0%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 23.9% · Safe to merge
    23.9%
    Repository