Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
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
  • Loading branch information
bnoordhuis committed Nov 14, 2017
commit 2e4aae4691b1de9dac34b227f1e3db9573ea6ce1
3 changes: 3 additions & 0 deletions lib/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,9 @@ function readPackage(requestPath) {
return false;
}

if (!/"main"/.test(json))
return packageMainCache[requestPath] = undefined;

try {
var pkg = packageMainCache[requestPath] = JSON.parse(json).main;
} catch (e) {
Expand Down
1 change: 0 additions & 1 deletion test/fixtures/packages/invalid/index.js

This file was deleted.

1 change: 0 additions & 1 deletion test/fixtures/packages/invalid/package.json

This file was deleted.

8 changes: 0 additions & 8 deletions test/sequential/test-module-loading.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,14 +100,6 @@ const d2 = require('../fixtures/b/d');
assert.notStrictEqual(threeFolder, three);
}

console.error('test package.json require() loading');
assert.throws(
function() {
require('../fixtures/packages/invalid');
},
/^SyntaxError: Error parsing .+: Unexpected token , in JSON at position 1$/
);

assert.strictEqual(require('../fixtures/packages/index').ok, 'ok',
'Failed loading package');
assert.strictEqual(require('../fixtures/packages/main').ok, 'ok',
Expand Down