Skip to content

Commit e53bf87

Browse files
committed
http,net: optimize repeated property access
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>
1 parent f00fb75 commit e53bf87

2 files changed

Lines changed: 25 additions & 20 deletions

File tree

‎lib/_http_agent.js‎

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -160,33 +160,36 @@ function Agent(options) {
160160

161161
this.options = { __proto__: null, ...options };
162162

163-
this.defaultPort = this.options.defaultPort || 80;
164-
this.protocol = this.options.protocol || 'http:';
163+
// Cache options for better performance
164+
const opts = this.options;
165165

166-
if (this.options.noDelay === undefined)
167-
this.options.noDelay = true;
166+
this.defaultPort = opts.defaultPort || 80;
167+
this.protocol = opts.protocol || 'http:';
168+
169+
if (opts.noDelay === undefined)
170+
opts.noDelay = true;
168171

169172
// Don't confuse net and make it think that we're connecting to a pipe
170-
this.options.path = null;
173+
opts.path = null;
171174
this.requests = { __proto__: null };
172175
this.sockets = { __proto__: null };
173176
this.freeSockets = { __proto__: null };
174-
this.keepAliveMsecs = this.options.keepAliveMsecs || 1000;
175-
this.keepAlive = this.options.keepAlive || false;
176-
this.maxSockets = this.options.maxSockets || Agent.defaultMaxSockets;
177-
this.maxFreeSockets = this.options.maxFreeSockets || 256;
178-
this.scheduling = this.options.scheduling || 'lifo';
179-
this.maxTotalSockets = this.options.maxTotalSockets;
177+
this.keepAliveMsecs = opts.keepAliveMsecs || 1000;
178+
this.keepAlive = opts.keepAlive || false;
179+
this.maxSockets = opts.maxSockets || Agent.defaultMaxSockets;
180+
this.maxFreeSockets = opts.maxFreeSockets || 256;
181+
this.scheduling = opts.scheduling || 'lifo';
182+
this.maxTotalSockets = opts.maxTotalSockets;
180183
this.totalSocketCount = 0;
181184

182185
this.agentKeepAliveTimeoutBuffer =
183-
typeof this.options.agentKeepAliveTimeoutBuffer === 'number' &&
184-
this.options.agentKeepAliveTimeoutBuffer >= 0 &&
185-
NumberIsFinite(this.options.agentKeepAliveTimeoutBuffer) ?
186-
this.options.agentKeepAliveTimeoutBuffer :
186+
typeof opts.agentKeepAliveTimeoutBuffer === 'number' &&
187+
opts.agentKeepAliveTimeoutBuffer >= 0 &&
188+
NumberIsFinite(opts.agentKeepAliveTimeoutBuffer) ?
189+
opts.agentKeepAliveTimeoutBuffer :
187190
1000;
188191

189-
const proxyEnv = this.options.proxyEnv;
192+
const proxyEnv = opts.proxyEnv;
190193
if (typeof proxyEnv === 'object' && proxyEnv !== null) {
191194
this[kProxyConfig] = parseProxyConfigFromEnv(proxyEnv, this.protocol, this.keepAlive);
192195
debug(`new ${this.protocol} agent with proxy config`, this[kProxyConfig]);

‎lib/net.js‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -836,9 +836,10 @@ Socket.prototype.setNoDelay = function(enable) {
836836
return this;
837837
}
838838

839-
if (this._handle.setNoDelay && enable !== this[kSetNoDelay]) {
839+
const handle = this._handle;
840+
if (handle.setNoDelay && enable !== this[kSetNoDelay]) {
840841
this[kSetNoDelay] = enable;
841-
this._handle.setNoDelay(enable);
842+
handle.setNoDelay(enable);
842843
}
843844

844845
return this;
@@ -867,7 +868,8 @@ Socket.prototype.setKeepAlive = function(enable, initialDelayMsecs,
867868
return this;
868869
}
869870

870-
if (!this._handle.setKeepAlive) {
871+
const handle = this._handle;
872+
if (!handle.setKeepAlive) {
871873
return this;
872874
}
873875

@@ -883,7 +885,7 @@ Socket.prototype.setKeepAlive = function(enable, initialDelayMsecs,
883885
this[kSetKeepAliveInitialDelay] = initialDelay;
884886
this[kSetKeepAliveInterval] = interval;
885887
this[kSetKeepAliveCount] = count;
886-
this._handle.setKeepAlive(enable, initialDelay, interval, count);
888+
handle.setKeepAlive(enable, initialDelay, interval, count);
887889
}
888890

889891
return this;

0 commit comments

Comments
 (0)