Skip to content

Commit 7c60328

Browse files
bnoordhuisjasnell
authored andcommitted
module: optimize js and json file i/o
Use internalModuleReadFile() to read the file from disk to avoid the fs.fstatSync() call that fs.readFileSync() makes. It does so to know the file size in advance so it doesn't have to allocate O(n) buffers when reading the file from disk. internalModuleReadFile() is plenty efficient though, even more so because we want a string and not a buffer. This way we also don't allocate a buffer that immediately gets thrown away again. This commit reduces the number of fstat() system calls in a benchmark application[0] from 549 to 29, all made by the application itself. [0] https://github.com/strongloop/loopback-sample-app PR-URL: #4575 Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 038b636 commit 7c60328

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

lib/module.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -428,14 +428,14 @@ Module.prototype._compile = function(content, filename) {
428428

429429
// Native extension for .js
430430
Module._extensions['.js'] = function(module, filename) {
431-
var content = fs.readFileSync(filename, 'utf8');
431+
var content = internalModuleReadFile(filename);
432432
module._compile(internalModule.stripBOM(content), filename);
433433
};
434434

435435

436436
// Native extension for .json
437437
Module._extensions['.json'] = function(module, filename) {
438-
var content = fs.readFileSync(filename, 'utf8');
438+
var content = internalModuleReadFile(filename);
439439
try {
440440
module.exports = JSON.parse(internalModule.stripBOM(content));
441441
} catch (err) {

0 commit comments

Comments
 (0)