Skip to content

Commit fdbb6dd

Browse files
committed
module: speed up package.json parsing
If the package.json does not contain the string '"main"', skip parsing it to JSON. Note that this changes the behavior of the module loader in the presence of package.json files that don't contain legal JSON. Such files used to throw an exception but now they are simply ignored unless they contain a "main" property. To me, that seems like a good trade-off: I observe a 25% reduction in start-up time on a medium-sized application[0]. [0] https://github.com/strongloop/sls-sample-app PR-URL: #15767 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 1132ea7 commit fdbb6dd

File tree

4 files changed

+3
-10
lines changed

4 files changed

+3
-10
lines changed

lib/module.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,9 @@ function readPackage(requestPath) {
120120
return false;
121121
}
122122

123+
if (!/"main"/.test(json))
124+
return packageMainCache[requestPath] = undefined;
125+
123126
try {
124127
var pkg = packageMainCache[requestPath] = JSON.parse(json).main;
125128
} catch (e) {

test/fixtures/packages/invalid/index.js

Lines changed: 0 additions & 1 deletion
This file was deleted.

test/fixtures/packages/invalid/package.json

Lines changed: 0 additions & 1 deletion
This file was deleted.

test/sequential/test-module-loading.js

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -100,14 +100,6 @@ const d2 = require('../fixtures/b/d');
100100
assert.notStrictEqual(threeFolder, three);
101101
}
102102

103-
console.error('test package.json require() loading');
104-
assert.throws(
105-
function() {
106-
require('../fixtures/packages/invalid');
107-
},
108-
/^SyntaxError: Error parsing .+: Unexpected token , in JSON at position 1$/
109-
);
110-
111103
assert.strictEqual(require('../fixtures/packages/index').ok, 'ok',
112104
'Failed loading package');
113105
assert.strictEqual(require('../fixtures/packages/main').ok, 'ok',

0 commit comments

Comments
 (0)