mirror of
https://github.com/nodejs/node.git
synced 2024-11-24 03:07:54 +01:00
fcf5de008a
PR-URL: https://github.com/nodejs/node/pull/49137 Refs: https://github.com/nodejs/node/pull/49079 Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
50 lines
1.1 KiB
JavaScript
50 lines
1.1 KiB
JavaScript
'use strict';
|
|
const fs = require('fs');
|
|
const common = require('../common.js');
|
|
|
|
const tmpdir = require('../../test/common/tmpdir');
|
|
const benchmarkDirectory = tmpdir.resolve('nodejs-benchmark-module');
|
|
|
|
const bench = common.createBenchmark(main, {
|
|
ext: ['', '.js'],
|
|
files: [1e3],
|
|
cache: ['true', 'false'],
|
|
});
|
|
|
|
function main({ ext, cache, files }) {
|
|
tmpdir.refresh();
|
|
fs.mkdirSync(benchmarkDirectory);
|
|
fs.writeFileSync(
|
|
`${benchmarkDirectory}/a.js`,
|
|
'module.exports = {};',
|
|
);
|
|
for (let i = 0; i <= files; i++) {
|
|
fs.mkdirSync(`${benchmarkDirectory}/${i}`);
|
|
fs.writeFileSync(
|
|
`${benchmarkDirectory}/${i}/package.json`,
|
|
'{"main": "index.js"}',
|
|
);
|
|
fs.writeFileSync(
|
|
`${benchmarkDirectory}/${i}/index.js`,
|
|
`require('../a${ext}');`,
|
|
);
|
|
}
|
|
|
|
measureDir(cache === 'true', files);
|
|
|
|
tmpdir.refresh();
|
|
}
|
|
|
|
function measureDir(cache, files) {
|
|
if (cache) {
|
|
for (let i = 0; i <= files; i++) {
|
|
require(`${benchmarkDirectory}/${i}`);
|
|
}
|
|
}
|
|
bench.start();
|
|
for (let i = 0; i <= files; i++) {
|
|
require(`${benchmarkDirectory}/${i}`);
|
|
}
|
|
bench.end(files);
|
|
}
|