Skip to content

Commit 2e4aae4

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
1 parent e7f30db commit 2e4aae4

File tree

4 files changed

+3
-10
lines changed

4 files changed

+3
-10
lines changed

lib/module.js

+3
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

-1
This file was deleted.

test/fixtures/packages/invalid/package.json

-1
This file was deleted.

test/sequential/test-module-loading.js

-8
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)