Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
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
80 changes: 80 additions & 0 deletions benchmark/module/module-require.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
'use strict';

const fs = require('fs');
const path = require('path');
const common = require('../common.js');
const tmpdir = require('../../test/common/tmpdir');
const benchmarkDirectory = path.join(tmpdir.path, 'nodejs-benchmark-module');

const bench = common.createBenchmark(main, {
type: ['.js', '.json', 'dir'],
n: [1e4],
});

function main({ type, n }) {
tmpdir.refresh();
createEntryPoint(n);

switch (type) {
case '.js':
measureJSFile(n);
break;
case '.json':
measureJSONFile(n);
break;
case 'dir':
measureDir(n);
}

tmpdir.refresh();
}

function measureJSFile(n) {
bench.start();
for (let i = 0; i < n; i++) {
require(`${benchmarkDirectory}/${i}`);
}
bench.end(n);
}

function measureJSONFile(n) {
bench.start();
for (let i = 0; i < n; i++) {
require(`${benchmarkDirectory}/json_${i}.json`);
}
bench.end(n);
}

function measureDir(n) {
bench.start();
for (let i = 0; i < n; i++) {
require(`${benchmarkDirectory}${i}`);
}
bench.end(n);
}

function createEntryPoint(n) {
fs.mkdirSync(benchmarkDirectory);

const JSFileContent = 'module.exports = [];';
const JSONFileContent = '[]';

for (let i = 0; i < n; i++) {
// JS file.
fs.writeFileSync(`${benchmarkDirectory}/${i}.js`, JSFileContent);

// JSON file.
fs.writeFileSync(`${benchmarkDirectory}/json_${i}.json`, JSONFileContent);

// Dir.
fs.mkdirSync(`${benchmarkDirectory}${i}`);
fs.writeFileSync(
`${benchmarkDirectory}${i}/package.json`,
'{"main": "index.js"}'
);
fs.writeFileSync(
`${benchmarkDirectory}${i}/index.js`,
JSFileContent
);
}
}
5 changes: 4 additions & 1 deletion lib/internal/modules/cjs/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,10 @@ function stat(filename) {
if (result !== undefined) return result;
}
const result = internalModuleStat(filename);
if (statCache !== null) statCache.set(filename, result);
if (statCache !== null && result >= 0) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Folders and files are AFAIK also handled separately and this only saves one of them in the cache?

Folders and files are both handled now. /cc @BridgeAR

// Only set cache when `internalModuleStat(filename)` succeeds.
statCache.set(filename, result);
}
return result;
}

Expand Down
19 changes: 19 additions & 0 deletions test/parallel/test-module-cache.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
'use strict';
require('../common');
const assert = require('assert');
const fs = require('fs');
const path = require('path');
const tmpdir = require('../common/tmpdir');
tmpdir.refresh();

const filePath = path.join(tmpdir.path, 'test-module-cache.json');
assert.throws(
() => require(filePath),
{ code: 'MODULE_NOT_FOUND' }
);

fs.writeFileSync(filePath, '[]');

const content = require(filePath);
assert.strictEqual(Array.isArray(content), true);
assert.strictEqual(content.length, 0);