Skip to content

Commit d89390f

Browse files
IlyasShabitargos
authored andcommitted
src: add cache to nearest parent package json
PR-URL: #59086 Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: Jacob Smith <jacob@frende.me> Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Marco Ippolito <marcoippolito54@gmail.com>
1 parent 83023e5 commit d89390f

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

lib/internal/modules/package_json_reader.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const {
55
JSONParse,
66
ObjectDefineProperty,
77
RegExpPrototypeExec,
8+
SafeMap,
89
StringPrototypeIndexOf,
910
StringPrototypeSlice,
1011
} = primordials;
@@ -28,6 +29,8 @@ const path = require('path');
2829
const { validateString } = require('internal/validators');
2930
const internalFsBinding = internalBinding('fs');
3031

32+
const nearestParentPackageJSONCache = new SafeMap();
33+
3134
/**
3235
* @typedef {import('typings/internalBinding/modules').DeserializedPackageConfig} DeserializedPackageConfig
3336
* @typedef {import('typings/internalBinding/modules').PackageConfig} PackageConfig
@@ -131,13 +134,21 @@ function read(jsonPath, { base, specifier, isESM } = kEmptyObject) {
131134
* @returns {undefined | DeserializedPackageConfig}
132135
*/
133136
function getNearestParentPackageJSON(checkPath) {
137+
if (nearestParentPackageJSONCache.has(checkPath)) {
138+
return nearestParentPackageJSONCache.get(checkPath);
139+
}
140+
134141
const result = modulesBinding.getNearestParentPackageJSON(checkPath);
135142

136143
if (result === undefined) {
144+
nearestParentPackageJSONCache.set(checkPath, undefined);
137145
return undefined;
138146
}
139147

140-
return deserializePackageJSON(checkPath, result);
148+
const packageConfig = deserializePackageJSON(checkPath, result);
149+
nearestParentPackageJSONCache.set(checkPath, packageConfig);
150+
151+
return packageConfig;
141152
}
142153

143154
/**

0 commit comments

Comments
 (0)