Skip to content

Instantly share code, notes, and snippets.

@isaacs
Created December 6, 2012 18:47
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save isaacs/4227004 to your computer and use it in GitHub Desktop.
Save isaacs/4227004 to your computer and use it in GitHub Desktop.
diff --git a/lib/module.js b/lib/module.js
index 6130394..80559f4 100644
--- a/lib/module.js
+++ b/lib/module.js
@@ -25,6 +25,7 @@ var runInThisContext = Script.runInThisContext;
var runInNewContext = Script.runInNewContext;
var assert = require('assert').ok;
+var timeRequire = +process.env.NODE_TIME_REQUIRE;
// If obj.hasOwnProperty has been overridden, then calling
// obj.hasOwnProperty(prop) will break.
@@ -359,7 +360,15 @@ Module.prototype.load = function(filename) {
Module.prototype.require = function(path) {
- return Module._load(path, this);
+ if (timeRequire) {
+ var start = process.hrtime();
+ }
+ var ret = Module._load(path, this);
+ if (timeRequire) {
+ var end = process.hrtime(start);
+ console.error(this.filename, path, end);
+ }
+ return ret;
};
@isaacs
Copy link
Author

isaacs commented Dec 6, 2012

Or monkey-patch:

var Module = require('module');
Module.prototype.require = function(path) {
  var start = process.hrtime();
  var ret = Module._load(path, this);
  var end = process.hrtime(start);
  console.error(this.filename, path, end);
  return ret;
};

but of course that'll only print stats for modules that have not been loaded yet.

@tj
Copy link

tj commented Dec 7, 2012

would be pretty cool if you could enable this or similar with NODE_DEBUG

@isaacs
Copy link
Author

isaacs commented Dec 8, 2012

@visionmedia Agreed. My only hesitation in landing a patch like this is that people get all excited and want to put a bunch of bullshit into the module system, and it's easier to say no if I also deny my own feature requests. But maybe something like NODE_DEBUG=module and match the pattern that we have in most of the other internal modules, that'd be nice.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment