Skip to content

Commit

Permalink
module: print amount of load time of a module
Browse files Browse the repository at this point in the history
  • Loading branch information
H4ad committed Mar 26, 2024
1 parent 1264414 commit bfd2f03
Showing 1 changed file with 22 additions and 2 deletions.
24 changes: 22 additions & 2 deletions lib/internal/modules/cjs/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -970,6 +970,8 @@ function getExportsForCircularRequire(module) {
* @param {boolean} isMain Whether the module is the main entry point
*/
Module._load = function(request, parent, isMain) {
const start = performance.now();

Check failure on line 973 in lib/internal/modules/cjs/loader.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

Unexpected use of 'performance'. Use `const { performance } = require('perf_hooks');` instead of the global

Check failure on line 973 in lib/internal/modules/cjs/loader.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

'performance' is not defined

let relResolveCacheIdentifier;
if (parent) {
debug('Module._load REQUEST %s parent: %s', request, parent.id);
Expand All @@ -984,8 +986,14 @@ Module._load = function(request, parent, isMain) {
if (cachedModule !== undefined) {
updateChildren(parent, cachedModule, true);
if (!cachedModule.loaded) {
return getExportsForCircularRequire(cachedModule);
const result = getExportsForCircularRequire(cachedModule);

debug(`TIMING [%s]: %d ms`, request, performance.now() - start);

Check failure on line 991 in lib/internal/modules/cjs/loader.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

Strings must use singlequote

Check failure on line 991 in lib/internal/modules/cjs/loader.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

Unexpected use of 'performance'. Use `const { performance } = require('perf_hooks');` instead of the global

Check failure on line 991 in lib/internal/modules/cjs/loader.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

'performance' is not defined

return result;
}

debug(`TIMING [%s]: %d ms`, request, performance.now() - start);

Check failure on line 996 in lib/internal/modules/cjs/loader.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

Strings must use singlequote

Check failure on line 996 in lib/internal/modules/cjs/loader.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

Unexpected use of 'performance'. Use `const { performance } = require('perf_hooks');` instead of the global

Check failure on line 996 in lib/internal/modules/cjs/loader.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

'performance' is not defined
return cachedModule.exports;
}
delete relativeResolveCache[relResolveCacheIdentifier];
Expand All @@ -1001,6 +1009,8 @@ Module._load = function(request, parent, isMain) {
}

const module = loadBuiltinModule(id, request);

debug(`TIMING [%s]: %d ms`, request, performance.now() - start);

Check failure on line 1013 in lib/internal/modules/cjs/loader.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

Strings must use singlequote

Check failure on line 1013 in lib/internal/modules/cjs/loader.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

Unexpected use of 'performance'. Use `const { performance } = require('perf_hooks');` instead of the global
return module.exports;
}

Expand All @@ -1011,16 +1021,24 @@ Module._load = function(request, parent, isMain) {
if (!cachedModule.loaded) {
const parseCachedModule = cjsSourceCache.get(cachedModule);
if (!parseCachedModule || parseCachedModule.loaded) {
return getExportsForCircularRequire(cachedModule);
const result = getExportsForCircularRequire(cachedModule);

debug(`TIMING [%s]: %d ms`, request, performance.now() - start);

return result;
}
parseCachedModule.loaded = true;
} else {
debug(`TIMING [%s]: %d ms`, request, performance.now() - start);
return cachedModule.exports;
}
}

if (BuiltinModule.canBeRequiredWithoutScheme(filename)) {
const mod = loadBuiltinModule(filename, request);

debug(`TIMING [%s]: %d ms`, request, performance.now() - start);

return mod.exports;
}

Expand Down Expand Up @@ -1068,6 +1086,8 @@ Module._load = function(request, parent, isMain) {
}
}

debug(`TIMING [%s]: %d ms`, request, performance.now() - start);

return module.exports;
};

Expand Down

0 comments on commit bfd2f03

Please sign in to comment.