If I use an inline require, like this:
function something(...paths) {
return require('path').join(...paths);
}
something('etc', 'etc');
Would the engine require in every call? Example:
let i = 10;
while (--i)
something(i, 'etc');
Thank you.
The system will call
require()
each time through your loop, but modules loaded withrequire()
are cached and the module loading code is only run the first time the module is loaded. So, while there is a slight bit of extra overhead callingrequire('path')
, it is only to look up that module name in the cache and return the cached module handle. It does not need to load, parse and run the module each time you callrequire()
.That said, it still would be better to be in the habit of this:
The other downside to the way you were doing it, is that the first time the
path
module is loaded, the system will use synchronous file I/O to load it which is not a good idea in a multi-user server. The file I/O just happens the first time, but still not a great practice. Better to get the synchronous I/O out of the way at server initialization time.