node icon indicating copy to clipboard operation
node copied to clipboard

http,net: optimize repeated property access

Open jbj338033 opened this issue 3 months ago • 5 comments

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

  • Reduces object traversal overhead in hot paths
  • Particularly beneficial for applications with high connection volumes
  • Follows the same optimization pattern as recent PRs (#59934, #59967)

Test plan

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

jbj338033 avatar Sep 22 '25 12:09 jbj338033

Review requested:

  • [ ] @nodejs/http
  • [ ] @nodejs/net

nodejs-github-bot avatar Sep 22 '25 12:09 nodejs-github-bot

Codecov Report

:white_check_mark: All modified and coverable lines are covered by tests. :white_check_mark: Project coverage is 88.44%. Comparing base (55cd2e5) to head (ca579ec). :warning: Report is 573 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%) :arrow_up:
lib/net.js 95.28% <100.00%> (+<0.01%) :arrow_up:

... and 31 files with indirect coverage changes

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

codecov[bot] avatar Sep 22 '25 13:09 codecov[bot]

CI: https://ci.nodejs.org/job/node-test-pull-request/69565/

nodejs-github-bot avatar Oct 05 '25 00:10 nodejs-github-bot

CI: https://ci.nodejs.org/job/node-test-pull-request/69567/

nodejs-github-bot avatar Oct 05 '25 10:10 nodejs-github-bot

CI: https://ci.nodejs.org/job/node-test-pull-request/70391/

nodejs-github-bot avatar Dec 04 '25 20:12 nodejs-github-bot