From 4bac1128e61058160116df3dd1467a3414b10224 Mon Sep 17 00:00:00 2001 From: Aarni Koskela Date: Fri, 10 Feb 2017 15:52:26 +0200 Subject: [PATCH 1/2] Instantiate default cache directory lazily, and only once When using `cacheDirectory=true`, the `findCacheDir` module is repeatedly called, and it does some synchronous filesystem calls under the hood. However, since `findCacheDir` is more or less a pure function, it's probably safe to call it only once during process lifetime. --- src/fs-cache.js | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/fs-cache.js b/src/fs-cache.js index 69530b4d..206c74a3 100644 --- a/src/fs-cache.js +++ b/src/fs-cache.js @@ -161,13 +161,19 @@ const handleCache = function(directory, params, callback) { * * }); */ + +var defaultCacheDirectory = null; // Lazily instantiated when needed + module.exports = function(params, callback) { let directory; if (typeof params.directory === "string") { directory = params.directory; } else { - directory = findCacheDir({ name: "babel-loader" }) || os.tmpdir(); + if (defaultCacheDirectory === null) { + defaultCacheDirectory = findCacheDir({ name: "babel-loader" }) || os.tmpdir(); + } + directory = defaultCacheDirectory; } handleCache(directory, params, callback); From 9b63ffb86f934732caafa8623b44b048574f1405 Mon Sep 17 00:00:00 2001 From: Aarni Koskela Date: Fri, 10 Feb 2017 15:54:06 +0200 Subject: [PATCH 2/2] Cache resolve-rc results --- src/fs-cache.js | 4 ++-- src/resolve-rc.js | 9 +++++++-- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/src/fs-cache.js b/src/fs-cache.js index 206c74a3..05b77130 100644 --- a/src/fs-cache.js +++ b/src/fs-cache.js @@ -15,6 +15,8 @@ const os = require("os"); const path = require("path"); const zlib = require("zlib"); +let defaultCacheDirectory = null; // Lazily instantiated when needed + /** * Read the contents from the compressed file. * @@ -162,8 +164,6 @@ const handleCache = function(directory, params, callback) { * }); */ -var defaultCacheDirectory = null; // Lazily instantiated when needed - module.exports = function(params, callback) { let directory; diff --git a/src/resolve-rc.js b/src/resolve-rc.js index 30b84f8b..25a03c6b 100644 --- a/src/resolve-rc.js +++ b/src/resolve-rc.js @@ -10,6 +10,8 @@ const path = require("path"); const exists = require("./utils/exists")({}); const read = require("./utils/read")({}); +const cache = {}; + const find = function find(start, rel) { const file = path.join(start, rel); @@ -27,6 +29,9 @@ const find = function find(start, rel) { module.exports = function(loc, rel) { rel = rel || ".babelrc"; - - return find(loc, rel); + const cacheKey = `${loc}/${rel}`; + if (!(cacheKey in cache)) { + cache[cacheKey] = find(loc, rel); + } + return cache[cacheKey]; };