Skip to content

Commit d864bbc

Browse files
committed
Lazily evaluate the properties
1 parent 71f4e6b commit d864bbc

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

lib/internal/modules/esm/initialize_import_meta.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,20 @@ function resolveModuleMeta(url) {
6666
return { __dirname: undefined, __filename: undefined };
6767
}
6868

69-
const filePath = fileURLToPath(url);
69+
// We return an object with getters to reduce memory usage in modules that
70+
// do not use these properties, i.e. most modules don't need the values
71+
// to be resolved until the first time the property is accessed.
72+
let filePath;
73+
let dirPath;
7074
return {
71-
__dirname: dirname(filePath),
72-
__filename: filePath,
75+
get __dirname() {
76+
return dirname(filePath ??= fileURLToPath(url));
77+
},
78+
get __filename() {
79+
filePath ??= fileURLToPath(url);
80+
dirPath ??= dirname(filePath);
81+
return filePath;
82+
},
7383
};
7484
}
7585

0 commit comments

Comments
 (0)