Skip to content

Commit b3575a0

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

File tree

1 file changed

+23
-3
lines changed

1 file changed

+23
-3
lines changed

lib/internal/modules/esm/initialize_import_meta.js

Lines changed: 23 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,27 @@ 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(this.filename);
71+
ObjectDefineProperty(this, 'dirname', { __proto__: null, value });
72+
return value;
73+
},
74+
__proto__: null,
75+
},
76+
filePath: {
77+
get() {
78+
const value = fileURLToPath(url);
79+
ObjectDefineProperty(this, 'filename', { __proto__: null, value });
80+
return value;
81+
},
82+
__proto__: null,
83+
},
84+
__proto__: null,
85+
});
6686
}
6787

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

0 commit comments

Comments
 (0)