@@ -61,6 +61,7 @@ const {
61
61
StringPrototypeSplit,
62
62
StringPrototypeStartsWith,
63
63
Symbol,
64
+ Number,
64
65
} = primordials ;
65
66
66
67
// Map used to store CJS parsing data or for ESM loading.
@@ -359,10 +360,22 @@ function setModuleParent(value) {
359
360
moduleParentCache . set ( this , value ) ;
360
361
}
361
362
362
- let debug = require ( 'internal/util/debuglog' ) . debuglog ( 'module' , ( fn ) => {
363
+ const { debuglog, isDebugEnabled } = require ( 'internal/util/debuglog' ) ;
364
+
365
+ let debug = debuglog ( 'module' , ( fn ) => {
363
366
debug = fn ;
364
367
} ) ;
365
368
369
+ let hrtimeBigIntTimingFn = ( ) => {
370
+ hrtimeBigIntTimingFn = isDebugEnabled ( 'timing_module_cjs' ) ? process . hrtime . bigint : ( ) => 0 ;
371
+
372
+ return hrtimeBigIntTimingFn ( ) ;
373
+ } ;
374
+
375
+ let debugTiming = debuglog ( 'timing_module_cjs' , ( fn ) => {
376
+ debugTiming = fn ;
377
+ } ) ;
378
+
366
379
ObjectDefineProperty ( Module . prototype , 'parent' , {
367
380
__proto__ : null ,
368
381
get : pendingDeprecate (
@@ -958,6 +971,10 @@ function getExportsForCircularRequire(module) {
958
971
return module . exports ;
959
972
}
960
973
974
+ function logTiming ( request , parent , start ) {
975
+ debugTiming ( '[%s] [%s]: %d ms' , parent ?. id || '' , request , Number ( ( hrtimeBigIntTimingFn ( ) - start ) ) / 1e6 ) ;
976
+ }
977
+
961
978
/**
962
979
* Load a module from cache if it exists, otherwise create a new module instance.
963
980
* 1. If a module already exists in the cache: return its exports object.
@@ -966,10 +983,12 @@ function getExportsForCircularRequire(module) {
966
983
* 3. Otherwise, create a new module for the file and save it to the cache.
967
984
* Then have it load the file contents before returning its exports object.
968
985
* @param {string } request Specifier of module to load via `require`
969
- * @param {string } parent Absolute path of the module importing the child
986
+ * @param {Module } parent Absolute path of the module importing the child
970
987
* @param {boolean } isMain Whether the module is the main entry point
971
988
*/
972
989
Module . _load = function ( request , parent , isMain ) {
990
+ const start = hrtimeBigIntTimingFn ( ) ;
991
+
973
992
let relResolveCacheIdentifier ;
974
993
if ( parent ) {
975
994
debug ( 'Module._load REQUEST %s parent: %s' , request , parent . id ) ;
@@ -984,8 +1003,14 @@ Module._load = function(request, parent, isMain) {
984
1003
if ( cachedModule !== undefined ) {
985
1004
updateChildren ( parent , cachedModule , true ) ;
986
1005
if ( ! cachedModule . loaded ) {
987
- return getExportsForCircularRequire ( cachedModule ) ;
1006
+ const result = getExportsForCircularRequire ( cachedModule ) ;
1007
+
1008
+ logTiming ( request , parent , start ) ;
1009
+
1010
+ return result ;
988
1011
}
1012
+
1013
+ logTiming ( request , parent , start ) ;
989
1014
return cachedModule . exports ;
990
1015
}
991
1016
delete relativeResolveCache [ relResolveCacheIdentifier ] ;
@@ -1001,6 +1026,8 @@ Module._load = function(request, parent, isMain) {
1001
1026
}
1002
1027
1003
1028
const module = loadBuiltinModule ( id , request ) ;
1029
+
1030
+ logTiming ( request , parent , start ) ;
1004
1031
return module . exports ;
1005
1032
}
1006
1033
@@ -1011,16 +1038,24 @@ Module._load = function(request, parent, isMain) {
1011
1038
if ( ! cachedModule . loaded ) {
1012
1039
const parseCachedModule = cjsSourceCache . get ( cachedModule ) ;
1013
1040
if ( ! parseCachedModule || parseCachedModule . loaded ) {
1014
- return getExportsForCircularRequire ( cachedModule ) ;
1041
+ const result = getExportsForCircularRequire ( cachedModule ) ;
1042
+
1043
+ logTiming ( request , parent , start ) ;
1044
+
1045
+ return result ;
1015
1046
}
1016
1047
parseCachedModule . loaded = true ;
1017
1048
} else {
1049
+ logTiming ( request , parent , start ) ;
1018
1050
return cachedModule . exports ;
1019
1051
}
1020
1052
}
1021
1053
1022
1054
if ( BuiltinModule . canBeRequiredWithoutScheme ( filename ) ) {
1023
1055
const mod = loadBuiltinModule ( filename , request ) ;
1056
+
1057
+ logTiming ( request , parent , start ) ;
1058
+
1024
1059
return mod . exports ;
1025
1060
}
1026
1061
@@ -1068,6 +1103,8 @@ Module._load = function(request, parent, isMain) {
1068
1103
}
1069
1104
}
1070
1105
1106
+ logTiming ( request , parent , start ) ;
1107
+
1071
1108
return module . exports ;
1072
1109
} ;
1073
1110
0 commit comments