Browse Source

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: https://github.com/nodejs/node/pull/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>
v6
Brian White 8 years ago
parent
commit
e32425bfcd
No known key found for this signature in database GPG Key ID: 606D7358F94DA209
  1. 9
      lib/module.js

9
lib/module.js

@ -165,10 +165,11 @@ Module._findPath = function(request, paths, isMain) {
return false; return false;
} }
const cacheKey = JSON.stringify({request: request, paths: paths}); var cacheKey = request + '\x00' +
if (Module._pathCache[cacheKey]) { (paths.length === 1 ? paths[0] : paths.join('\x00'));
return Module._pathCache[cacheKey]; var entry = Module._pathCache[cacheKey];
} if (entry)
return entry;
var exts; var exts;
var trailingSlash = request.length > 0 && var trailingSlash = request.length > 0 &&

Loading…
Cancel
Save