-
Notifications
You must be signed in to change notification settings - Fork 30k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
module: only set cache when finding module succeeds
PR-URL: #36642 Fixes: #36638 Reviewed-By: Bradley Farias <[email protected]> Reviewed-By: Guy Bedford <[email protected]> Reviewed-By: Antoine du Hamel <[email protected]> Reviewed-By: Michaël Zasso <[email protected]> Reviewed-By: James M Snell <[email protected]>
- Loading branch information
zhangyongsheng
committed
Feb 12, 2021
1 parent
f2c2615
commit aecd5eb
Showing
3 changed files
with
103 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |