|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +// require() of a package that is already loaded, from a directory other than |
| 4 | +// the one that first loaded it, with and without package.json "exports". |
| 5 | + |
| 6 | +const fs = require('fs'); |
| 7 | +const path = require('path'); |
| 8 | +const { createRequire } = require('module'); |
| 9 | +const common = require('../common.js'); |
| 10 | +const tmpdir = require('../../test/common/tmpdir'); |
| 11 | + |
| 12 | +const bench = common.createBenchmark(main, { |
| 13 | + exports: ['true', 'false'], |
| 14 | + n: [1e5], |
| 15 | +}); |
| 16 | + |
| 17 | +function main({ exports, n }) { |
| 18 | + tmpdir.refresh(); |
| 19 | + const root = tmpdir.resolve('nodejs-benchmark-module-exports'); |
| 20 | + const pkgDir = path.join(root, 'node_modules', 'pkg'); |
| 21 | + const consumerDir = path.join(root, 'node_modules', 'consumer'); |
| 22 | + fs.mkdirSync(pkgDir, { recursive: true }); |
| 23 | + fs.mkdirSync(consumerDir); |
| 24 | + |
| 25 | + const manifest = { name: 'pkg', main: 'index.js' }; |
| 26 | + if (exports === 'true') { |
| 27 | + manifest.exports = './index.js'; |
| 28 | + } |
| 29 | + fs.writeFileSync(path.join(pkgDir, 'package.json'), JSON.stringify(manifest)); |
| 30 | + fs.writeFileSync(path.join(pkgDir, 'index.js'), 'module.exports = {};'); |
| 31 | + fs.writeFileSync(path.join(consumerDir, 'index.js'), |
| 32 | + 'module.exports = () => require("pkg");'); |
| 33 | + |
| 34 | + const rootRequire = createRequire(path.join(root, 'index.js')); |
| 35 | + rootRequire('pkg'); |
| 36 | + const load = rootRequire('consumer'); |
| 37 | + |
| 38 | + bench.start(); |
| 39 | + for (let i = 0; i < n; i++) { |
| 40 | + load(); |
| 41 | + } |
| 42 | + bench.end(n); |
| 43 | +} |
0 commit comments