From e32425bfcd55497cdad4982908d6fcba9a0e033c Mon Sep 17 00:00:00 2001 From: Brian White Date: Fri, 13 Jan 2017 05:19:54 -0500 Subject: [PATCH] module: avoid JSON.stringify() for cache key MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 Reviewed-By: Michaƫl Zasso Reviewed-By: Michael Dawson Reviewed-By: Matteo Collina Reviewed-By: Benjamin Gruenbaum --- lib/module.js | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/lib/module.js b/lib/module.js index 1b9c2413b7..77675fcd98 100644 --- a/lib/module.js +++ b/lib/module.js @@ -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 &&