Skip to content

Commit f7e69ab

Browse files
akxdanez
authored andcommitted
FS caching improvements (#375)
* 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. * Cache resolve-rc results
1 parent fd090c9 commit f7e69ab

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

src/fs-cache.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ const os = require("os");
1515
const path = require("path");
1616
const zlib = require("zlib");
1717

18+
let defaultCacheDirectory = null; // Lazily instantiated when needed
19+
1820
/**
1921
* Read the contents from the compressed file.
2022
*
@@ -161,13 +163,17 @@ const handleCache = function(directory, params, callback) {
161163
*
162164
* });
163165
*/
166+
164167
module.exports = function(params, callback) {
165168
let directory;
166169

167170
if (typeof params.directory === "string") {
168171
directory = params.directory;
169172
} else {
170-
directory = findCacheDir({ name: "babel-loader" }) || os.tmpdir();
173+
if (defaultCacheDirectory === null) {
174+
defaultCacheDirectory = findCacheDir({ name: "babel-loader" }) || os.tmpdir();
175+
}
176+
directory = defaultCacheDirectory;
171177
}
172178

173179
handleCache(directory, params, callback);

src/resolve-rc.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ const path = require("path");
1010
const exists = require("./utils/exists")({});
1111
const read = require("./utils/read")({});
1212

13+
const cache = {};
14+
1315
const find = function find(start, rel) {
1416
const file = path.join(start, rel);
1517

@@ -27,6 +29,9 @@ const find = function find(start, rel) {
2729

2830
module.exports = function(loc, rel) {
2931
rel = rel || ".babelrc";
30-
31-
return find(loc, rel);
32+
const cacheKey = `${loc}/${rel}`;
33+
if (!(cacheKey in cache)) {
34+
cache[cacheKey] = find(loc, rel);
35+
}
36+
return cache[cacheKey];
3237
};

0 commit comments

Comments
 (0)