Skip to content

Conversation

jbj338033
Copy link

Summary

  • Cache frequently accessed object properties in local variables to reduce property lookup overhead
  • Optimizes HTTP Agent constructor and net.Socket configuration methods

Details

This PR optimizes repeated property access patterns in network-related modules:

HTTP Agent Constructor (lib/_http_agent.js)

  • Caches this.options in a local opts variable
  • Eliminates 14+ repeated property access chains during Agent instantiation
  • HTTP Agents are created frequently for HTTP/HTTPS requests

Socket Configuration Methods (lib/net.js)

  • setNoDelay(): Caches this._handle to avoid 2 property lookups
  • setKeepAlive(): Caches this._handle to avoid 2 property lookups
  • These methods are called during socket setup for every TCP connection

Performance Impact

Test plan

  • Existing HTTP/HTTPS tests pass
  • Existing net tests pass
  • No functional changes, only performance optimizations
  • Manually verified Agent creation and socket configuration work correctly

Cache frequently accessed properties to reduce property lookup overhead:

- HTTP Agent constructor: Cache `this.options` in local variable to
  eliminate 14+ property access chains during initialization
- net.Socket.setNoDelay: Cache `this._handle` to avoid repeated access
- net.Socket.setKeepAlive: Cache `this._handle` to avoid repeated access

These optimizations target hot paths in network operations, reducing
object traversal overhead during socket configuration and HTTP agent
instantiation.

Signed-off-by: jbj338033 <jbj338033@gmail.com>
@nodejs-github-bot
Copy link
Collaborator

Review requested:

  • @nodejs/http
  • @nodejs/net

@nodejs-github-bot nodejs-github-bot added http Issues or PRs related to the http subsystem. needs-ci PRs that need a full CI run. net Issues and PRs related to the net subsystem. labels Sep 22, 2025
Copy link
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

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

lgtm

Copy link

codecov bot commented Sep 22, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 88.44%. Comparing base (55cd2e5) to head (ca579ec).
⚠️ Report is 107 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #59968      +/-   ##
==========================================
+ Coverage   88.41%   88.44%   +0.02%     
==========================================
  Files         703      703              
  Lines      207421   207426       +5     
  Branches    39993    40012      +19     
==========================================
+ Hits       183398   183448      +50     
+ Misses      15996    15945      -51     
- Partials     8027     8033       +6     
Files with missing lines Coverage Δ
lib/_http_agent.js 97.33% <100.00%> (+0.01%) ⬆️
lib/net.js 95.28% <100.00%> (+<0.01%) ⬆️

... and 31 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@anonrig anonrig added the request-ci Add this label to start a Jenkins CI on a PR. label Oct 5, 2025
@github-actions github-actions bot removed the request-ci Add this label to start a Jenkins CI on a PR. label Oct 5, 2025
@nodejs-github-bot
Copy link
Collaborator

@nodejs-github-bot
Copy link
Collaborator

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

http Issues or PRs related to the http subsystem. needs-ci PRs that need a full CI run. net Issues and PRs related to the net subsystem.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants