Skip to content

Commit e32425b

Browse files
committed
module: avoid JSON.stringify() for cache key
By avoiding JSON.stringify() and simply joining the strings with a delimiter that does not appear in paths, we can improve cached require() performance by at least 50%. Additionally, this commit removes the last source of permanent function deoptimization (const) for _findPath(). PR-URL: #10789 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Michaël Zasso <[email protected]> Reviewed-By: Michael Dawson <[email protected]> Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Benjamin Gruenbaum <[email protected]>
1 parent 28dc848 commit e32425b

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

‎lib/module.js‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -165,10 +165,11 @@ Module._findPath = function(request, paths, isMain){
165165
returnfalse;
166166
}
167167

168-
constcacheKey=JSON.stringify({request: request,paths: paths});
169-
if(Module._pathCache[cacheKey]){
170-
returnModule._pathCache[cacheKey];
171-
}
168+
varcacheKey=request+'\x00'+
169+
(paths.length===1 ? paths[0] : paths.join('\x00'));
170+
varentry=Module._pathCache[cacheKey];
171+
if(entry)
172+
returnentry;
172173

173174
varexts;
174175
vartrailingSlash=request.length>0&&

0 commit comments

Comments
(0)