Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/nodejs/node.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorzhangyongsheng <zhangyongsheng@youzan.com>2021-01-17 16:18:45 +0300
committerzhangyongsheng <zhangyongsheng@youzan.com>2021-02-12 17:08:40 +0300
commitaecd5ebf49c9c4a8a399d5f56d1705288b84ea36 (patch)
treeba74118453eb73fdef4fd061cabfeda4429fa162 /benchmark
parentf2c26153d79564afe893d8135be616af402d0136 (diff)
module: only set cache when finding module succeeds
PR-URL: https://github.com/nodejs/node/pull/36642 Fixes: https://github.com/nodejs/node/issues/36638 Reviewed-By: Bradley Farias <bradley.meck@gmail.com> Reviewed-By: Guy Bedford <guybedford@gmail.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: Michaƫl Zasso <targos@protonmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
Diffstat (limited to 'benchmark')
-rw-r--r--benchmark/module/module-require.js80
1 files changed, 80 insertions, 0 deletions
diff --git a/benchmark/module/module-require.js b/benchmark/module/module-require.js
new file mode 100644
index 00000000000..d5b72c4b03f
--- /dev/null
+++ b/benchmark/module/module-require.js
@@ -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
+ );
+ }
+}