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

+5-4
Original file line numberDiff line numberDiff line change
@@ -165,10 +165,11 @@ Module._findPath = function(request, paths, isMain) {
165165
return false;
166166
}
167167

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

173174
var exts;
174175
var trailingSlash = request.length > 0 &&

0 commit comments

Comments
 (0)