Skip to content

Commit

Permalink
module: avoid JSON.stringify() for cache key
Browse files Browse the repository at this point in the history
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 <jasnell@gmail.com>
Reviewed-By: Michaël Zasso <targos@protonmail.com>
Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
  • Loading branch information
mscdex committed Mar 11, 2017
1 parent 28dc848 commit e32425b
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions lib/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,10 +165,11 @@ Module._findPath = function(request, paths, isMain) {
return false;
}

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

var exts;
var trailingSlash = request.length > 0 &&
Expand Down

0 comments on commit e32425b

Please sign in to comment.