| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent f2c2615 commit aecd5eb
3 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,80 @@ | |||
| 1 | + 'use strict'; | ||
| 2 | + | ||
| 3 | + const fs = require('fs'); | ||
| 4 | + const path = require('path'); | ||
| 5 | + const common = require('../common.js'); | ||
| 6 | + const tmpdir = require('../../test/common/tmpdir'); | ||
| 7 | + const benchmarkDirectory = path.join(tmpdir.path, 'nodejs-benchmark-module'); | ||
| 8 | + | ||
| 9 | + const bench = common.createBenchmark(main, { | ||
| 10 | + type: ['.js', '.json', 'dir'], | ||
| 11 | + n: [1e4], | ||
| 12 | + }); | ||
| 13 | + | ||
| 14 | + function main({ type, n }) { | ||
| 15 | + tmpdir.refresh(); | ||
| 16 | + createEntryPoint(n); | ||
| 17 | + | ||
| 18 | + switch (type) { | ||
| 19 | + case '.js': | ||
| 20 | + measureJSFile(n); | ||
| 21 | + break; | ||
| 22 | + case '.json': | ||
| 23 | + measureJSONFile(n); | ||
| 24 | + break; | ||
| 25 | + case 'dir': | ||
| 26 | + measureDir(n); | ||
| 27 | + } | ||
| 28 | + | ||
| 29 | + tmpdir.refresh(); | ||
| 30 | + } | ||
| 31 | + | ||
| 32 | + function measureJSFile(n) { | ||
| 33 | + bench.start(); | ||
| 34 | + for (let i = 0; i < n; i++) { | ||
| 35 | + require(`${benchmarkDirectory}/${i}`); | ||
| 36 | + } | ||
| 37 | + bench.end(n); | ||
| 38 | + } | ||
| 39 | + | ||
| 40 | + function measureJSONFile(n) { | ||
| 41 | + bench.start(); | ||
| 42 | + for (let i = 0; i < n; i++) { | ||
| 43 | + require(`${benchmarkDirectory}/json_${i}.json`); | ||
| 44 | + } | ||
| 45 | + bench.end(n); | ||
| 46 | + } | ||
| 47 | + | ||
| 48 | + function measureDir(n) { | ||
| 49 | + bench.start(); | ||
| 50 | + for (let i = 0; i < n; i++) { | ||
| 51 | + require(`${benchmarkDirectory}${i}`); | ||
| 52 | + } | ||
| 53 | + bench.end(n); | ||
| 54 | + } | ||
| 55 | + | ||
| 56 | + function createEntryPoint(n) { | ||
| 57 | + fs.mkdirSync(benchmarkDirectory); | ||
| 58 | + | ||
| 59 | + const JSFileContent = 'module.exports = [];'; | ||
| 60 | + const JSONFileContent = '[]'; | ||
| 61 | + | ||
| 62 | + for (let i = 0; i < n; i++) { | ||
| 63 | + // JS file. | ||
| 64 | + fs.writeFileSync(`${benchmarkDirectory}/${i}.js`, JSFileContent); | ||
| 65 | + | ||
| 66 | + // JSON file. | ||
| 67 | + fs.writeFileSync(`${benchmarkDirectory}/json_${i}.json`, JSONFileContent); | ||
| 68 | + | ||
| 69 | + // Dir. | ||
| 70 | + fs.mkdirSync(`${benchmarkDirectory}${i}`); | ||
| 71 | + fs.writeFileSync( | ||
| 72 | + `${benchmarkDirectory}${i}/package.json`, | ||
| 73 | + '{"main": "index.js"}' | ||
| 74 | + ); | ||
| 75 | + fs.writeFileSync( | ||
| 76 | + `${benchmarkDirectory}${i}/index.js`, | ||
| 77 | + JSFileContent | ||
| 78 | + ); | ||
| 79 | + } | ||
| 80 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -148,7 +148,10 @@ function stat(filename) { | |||
| 148 | 148 | if (result !== undefined) return result; | |
| 149 | 149 | } | |
| 150 | 150 | const result = internalModuleStat(filename); | |
| 151 | - if (statCache !== null) statCache.set(filename, result); | ||
| 151 | + if (statCache !== null && result >= 0) { | ||
| 152 | + // Only set cache when `internalModuleStat(filename)` succeeds. | ||
| 153 | + statCache.set(filename, result); | ||
| 154 | + } | ||
| 152 | 155 | return result; | |
| 153 | 156 | } | |
| 154 | 157 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,19 @@ | |||
| 1 | + 'use strict'; | ||
| 2 | + require('../common'); | ||
| 3 | + const assert = require('assert'); | ||
| 4 | + const fs = require('fs'); | ||
| 5 | + const path = require('path'); | ||
| 6 | + const tmpdir = require('../common/tmpdir'); | ||
| 7 | + tmpdir.refresh(); | ||
| 8 | + | ||
| 9 | + const filePath = path.join(tmpdir.path, 'test-module-cache.json'); | ||
| 10 | + assert.throws( | ||
| 11 | + () => require(filePath), | ||
| 12 | + { code: 'MODULE_NOT_FOUND' } | ||
| 13 | + ); | ||
| 14 | + | ||
| 15 | + fs.writeFileSync(filePath, '[]'); | ||
| 16 | + | ||
| 17 | + const content = require(filePath); | ||
| 18 | + assert.strictEqual(Array.isArray(content), true); | ||
| 19 | + assert.strictEqual(content.length, 0); | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments