Skip to content

Commit 90d6aac

Browse files
esm: avoid import.meta setup costs for unused properties
1 parent 3186468 commit 90d6aac

File tree

1 file changed

+20
-3
lines changed

1 file changed

+20
-3
lines changed

lib/internal/modules/esm/initialize_import_meta.js

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
'use strict';
22

33
const {
4+
ObjectDefineProperties,
5+
ObjectDefineProperty,
46
StringPrototypeStartsWith,
57
} = primordials;
68

@@ -60,9 +62,24 @@ function initializeImportMeta(meta, context, loader) {
6062
if (StringPrototypeStartsWith(url, 'file:') === true) {
6163
// These only make sense for locally loaded modules,
6264
// i.e. network modules are not supported.
63-
const filePath = fileURLToPath(url);
64-
meta.dirname = dirname(filePath);
65-
meta.filename = filePath;
65+
66+
// Avoid paying the cost to derive these unless they're actually accessed.
67+
ObjectDefineProperties(meta, {
68+
dirname: {
69+
get() {
70+
const value = dirname(filePath);
71+
ObjectDefineProperty(this, 'dirname', { __proto__: null, value });
72+
return value;
73+
},
74+
},
75+
filePath: {
76+
get() {
77+
const value = fileURLToPath(url);
78+
ObjectDefineProperty(this, 'filename', { __proto__: null, value });
79+
return value;
80+
},
81+
},
82+
});
6683
}
6784

6885
if (!loader || loader.allowImportMetaResolve) {

0 commit comments

Comments
 (0)