Skip to content

Commit ca579ec

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 <[email protected]>
1 parent 3e79dba commit ca579ec

File tree

2 files changed

+25
-20
lines changed

2 files changed

+25
-20
lines changed

lib/_http_agent.js

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

9696
this.options = { __proto__: null, ...options };
9797

98-
this.defaultPort = this.options.defaultPort || 80;
99-
this.protocol = this.options.protocol || 'http:';
98+
// Cache options for better performance
99+
const opts = this.options;
100100

101-
if (this.options.noDelay === undefined)
102-
this.options.noDelay = true;
101+
this.defaultPort = opts.defaultPort || 80;
102+
this.protocol = opts.protocol || 'http:';
103+
104+
if (opts.noDelay === undefined)
105+
opts.noDelay = true;
103106

104107
// Don't confuse net and make it think that we're connecting to a pipe
105-
this.options.path = null;
108+
opts.path = null;
106109
this.requests = { __proto__: null };
107110
this.sockets = { __proto__: null };
108111
this.freeSockets = { __proto__: null };
109-
this.keepAliveMsecs = this.options.keepAliveMsecs || 1000;
110-
this.keepAlive = this.options.keepAlive || false;
111-
this.maxSockets = this.options.maxSockets || Agent.defaultMaxSockets;
112-
this.maxFreeSockets = this.options.maxFreeSockets || 256;
113-
this.scheduling = this.options.scheduling || 'lifo';
114-
this.maxTotalSockets = this.options.maxTotalSockets;
112+
this.keepAliveMsecs = opts.keepAliveMsecs || 1000;
113+
this.keepAlive = opts.keepAlive || false;
114+
this.maxSockets = opts.maxSockets || Agent.defaultMaxSockets;
115+
this.maxFreeSockets = opts.maxFreeSockets || 256;
116+
this.scheduling = opts.scheduling || 'lifo';
117+
this.maxTotalSockets = opts.maxTotalSockets;
115118
this.totalSocketCount = 0;
116119

117120
this.agentKeepAliveTimeoutBuffer =
118-
typeof this.options.agentKeepAliveTimeoutBuffer === 'number' &&
119-
this.options.agentKeepAliveTimeoutBuffer >= 0 &&
120-
NumberIsFinite(this.options.agentKeepAliveTimeoutBuffer) ?
121-
this.options.agentKeepAliveTimeoutBuffer :
121+
typeof opts.agentKeepAliveTimeoutBuffer === 'number' &&
122+
opts.agentKeepAliveTimeoutBuffer >= 0 &&
123+
NumberIsFinite(opts.agentKeepAliveTimeoutBuffer) ?
124+
opts.agentKeepAliveTimeoutBuffer :
122125
1000;
123126

124-
const proxyEnv = this.options.proxyEnv;
127+
const proxyEnv = opts.proxyEnv;
125128
if (typeof proxyEnv === 'object' && proxyEnv !== null) {
126129
this[kProxyConfig] = parseProxyConfigFromEnv(proxyEnv, this.protocol, this.keepAlive);
127130
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
@@ -614,9 +614,10 @@ Socket.prototype.setNoDelay = function(enable) {
614614
return this;
615615
}
616616

617-
if (this._handle.setNoDelay && enable !== this[kSetNoDelay]) {
617+
const handle = this._handle;
618+
if (handle.setNoDelay && enable !== this[kSetNoDelay]) {
618619
this[kSetNoDelay] = enable;
619-
this._handle.setNoDelay(enable);
620+
handle.setNoDelay(enable);
620621
}
621622

622623
return this;
@@ -633,7 +634,8 @@ Socket.prototype.setKeepAlive = function(enable, initialDelayMsecs) {
633634
return this;
634635
}
635636

636-
if (!this._handle.setKeepAlive) {
637+
const handle = this._handle;
638+
if (!handle.setKeepAlive) {
637639
return this;
638640
}
639641

@@ -645,7 +647,7 @@ Socket.prototype.setKeepAlive = function(enable, initialDelayMsecs) {
645647
) {
646648
this[kSetKeepAlive] = enable;
647649
this[kSetKeepAliveInitialDelay] = initialDelay;
648-
this._handle.setKeepAlive(enable, initialDelay);
650+
handle.setKeepAlive(enable, initialDelay);
649651
}
650652

651653
return this;

0 commit comments

Comments
 (0)