Skip to content

Commit 70ef563

Browse files
committed
module: print amount of load time of a module
1 parent 1264414 commit 70ef563

File tree

1 file changed

+24
-3
lines changed

1 file changed

+24
-3
lines changed

lib/internal/modules/cjs/loader.js

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ const {
106106
const assert = require('internal/assert');
107107
const fs = require('fs');
108108
const path = require('path');
109+
const { performance } = require('perf_hooks');
109110
const { internalModuleStat } = internalBinding('fs');
110111
const { safeGetenv } = internalBinding('credentials');
111112
const {
@@ -966,10 +967,12 @@ function getExportsForCircularRequire(module) {
966967
* 3. Otherwise, create a new module for the file and save it to the cache.
967968
* Then have it load the file contents before returning its exports object.
968969
* @param {string} request Specifier of module to load via `require`
969-
* @param {string} parent Absolute path of the module importing the child
970+
* @param {Module} parent Absolute path of the module importing the child
970971
* @param {boolean} isMain Whether the module is the main entry point
971972
*/
972973
Module._load = function(request, parent, isMain) {
974+
const start = performance.now();
975+
973976
let relResolveCacheIdentifier;
974977
if (parent) {
975978
debug('Module._load REQUEST %s parent: %s', request, parent.id);
@@ -984,8 +987,14 @@ Module._load = function(request, parent, isMain) {
984987
if (cachedModule !== undefined) {
985988
updateChildren(parent, cachedModule, true);
986989
if (!cachedModule.loaded) {
987-
return getExportsForCircularRequire(cachedModule);
990+
const result = getExportsForCircularRequire(cachedModule);
991+
992+
debug('TIMING [%s] [%s]: %d ms', parent?.id || '', request, performance.now() - start);
993+
994+
return result;
988995
}
996+
997+
debug('TIMING [%s] [%s]: %d ms', parent?.id || '', request, performance.now() - start);
989998
return cachedModule.exports;
990999
}
9911000
delete relativeResolveCache[relResolveCacheIdentifier];
@@ -1001,6 +1010,8 @@ Module._load = function(request, parent, isMain) {
10011010
}
10021011

10031012
const module = loadBuiltinModule(id, request);
1013+
1014+
debug('TIMING [%s] [%s]: %d ms', parent?.id || '', request, performance.now() - start);
10041015
return module.exports;
10051016
}
10061017

@@ -1011,16 +1022,24 @@ Module._load = function(request, parent, isMain) {
10111022
if (!cachedModule.loaded) {
10121023
const parseCachedModule = cjsSourceCache.get(cachedModule);
10131024
if (!parseCachedModule || parseCachedModule.loaded) {
1014-
return getExportsForCircularRequire(cachedModule);
1025+
const result = getExportsForCircularRequire(cachedModule);
1026+
1027+
debug('TIMING [%s] [%s]: %d ms', parent?.id || '', request, performance.now() - start);
1028+
1029+
return result;
10151030
}
10161031
parseCachedModule.loaded = true;
10171032
} else {
1033+
debug('TIMING [%s] [%s]: %d ms', parent?.id || '', request, performance.now() - start);
10181034
return cachedModule.exports;
10191035
}
10201036
}
10211037

10221038
if (BuiltinModule.canBeRequiredWithoutScheme(filename)) {
10231039
const mod = loadBuiltinModule(filename, request);
1040+
1041+
debug('TIMING [%s] [%s]: %d ms', parent?.id || '', request, performance.now() - start);
1042+
10241043
return mod.exports;
10251044
}
10261045

@@ -1068,6 +1087,8 @@ Module._load = function(request, parent, isMain) {
10681087
}
10691088
}
10701089

1090+
debug('TIMING [%s] [%s]: %d ms', parent?.id || '', request, performance.now() - start);
1091+
10711092
return module.exports;
10721093
};
10731094

0 commit comments

Comments
 (0)