Skip to content

Commit afb6802

Browse files
committed
https: optimize session cache property access
Cache repeated property accesses in _cacheSession and _evictSession methods to improve performance. This reduces the number of property lookups from 7 to 1 in _cacheSession and from 2 to 1 in _evictSession. The optimization follows a similar pattern to other performance improvements in the codebase by caching frequently accessed object properties in local variables. Signed-off-by: jbj338033 <[email protected]>
1 parent 3e79dba commit afb6802

File tree

1 file changed

+14
-10
lines changed

1 file changed

+14
-10
lines changed

lib/https.js

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -572,30 +572,34 @@ Agent.prototype._cacheSession = function _cacheSession(key, session) {
572572
if (this.maxCachedSessions === 0)
573573
return;
574574

575+
// Cache property accesses for better performance
576+
const { map, list } = this._sessionCache;
577+
575578
// Fast case - update existing entry
576-
if (this._sessionCache.map[key]) {
577-
this._sessionCache.map[key] = session;
579+
if (map[key]) {
580+
map[key] = session;
578581
return;
579582
}
580583

581584
// Put new entry
582-
if (this._sessionCache.list.length >= this.maxCachedSessions) {
583-
const oldKey = ArrayPrototypeShift(this._sessionCache.list);
585+
if (list.length >= this.maxCachedSessions) {
586+
const oldKey = ArrayPrototypeShift(list);
584587
debug('evicting %j', oldKey);
585-
delete this._sessionCache.map[oldKey];
588+
delete map[oldKey];
586589
}
587590

588-
ArrayPrototypePush(this._sessionCache.list, key);
589-
this._sessionCache.map[key] = session;
591+
ArrayPrototypePush(list, key);
592+
map[key] = session;
590593
};
591594

592595
Agent.prototype._evictSession = function _evictSession(key) {
593-
const index = ArrayPrototypeIndexOf(this._sessionCache.list, key);
596+
const { map, list } = this._sessionCache;
597+
const index = ArrayPrototypeIndexOf(list, key);
594598
if (index === -1)
595599
return;
596600

597-
ArrayPrototypeSplice(this._sessionCache.list, index, 1);
598-
delete this._sessionCache.map[key];
601+
ArrayPrototypeSplice(list, index, 1);
602+
delete map[key];
599603
};
600604

601605
const globalAgent = new Agent({

0 commit comments

Comments
 (0)