Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 28 additions & 6 deletions benchmark/fs/bench-glob.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
'use strict';

const common = require('../common');
const fs = require('fs');
const {
glob,
globSync,
promises: { glob: globAsync },
} = require('fs');
const path = require('path');
const assert = require('node:assert');

Expand All @@ -11,7 +15,7 @@ const configs = {
n: [1e3],
dir: ['lib'],
pattern: ['**/*', '*.js', '**/**.js'],
mode: ['async', 'sync'],
mode: ['sync', 'promise', 'callback'],
recursive: ['true', 'false'],
};

Expand All @@ -20,15 +24,33 @@ const bench = common.createBenchmark(main, configs);
async function main(config) {
const fullPath = path.resolve(benchmarkDirectory, config.dir);
const { pattern, recursive, mode } = config;
const options = { cwd: fullPath, recursive };
const callback = (resolve, reject) => {
glob(pattern, options, (err, matches) => {
if (err) {
reject(err);
} else {
resolve(matches);
}
});
};

let noDead;
bench.start();

for (let i = 0; i < config.n; i++) {
if (mode === 'async') {
noDead = await fs.promises.glob(pattern, { cwd: fullPath, recursive });
} else {
noDead = fs.globSync(pattern, { cwd: fullPath, recursive });
switch (mode) {
case 'sync':
noDead = globSync(pattern, options);
break;
case 'promise':
noDead = await globAsync(pattern, options);
break;
case 'callback':
noDead = await new Promise(callback);
break;
default:
throw new Error(`Unknown mode: ${mode}`);
}
}

Expand Down
Loading