Skip to content

Commit 46aa079

Browse files
LiviaMedeirostargos
authored andcommitted
benchmark: add callback-based fs.glob to glob benchmark
PR-URL: #58417 Reviewed-By: Vinícius Lourenço Claro Cardoso <[email protected]> Reviewed-By: Rafael Gonzaga <[email protected]>
1 parent cc1aaca commit 46aa079

File tree

1 file changed

+28
-6
lines changed

1 file changed

+28
-6
lines changed

benchmark/fs/bench-glob.js

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
'use strict';
22

33
const common = require('../common');
4-
const fs = require('fs');
4+
const {
5+
glob,
6+
globSync,
7+
promises: { glob: globAsync },
8+
} = require('fs');
59
const path = require('path');
610
const assert = require('node:assert');
711

@@ -11,7 +15,7 @@ const configs = {
1115
n: [1e3],
1216
dir: ['lib'],
1317
pattern: ['**/*', '*.js', '**/**.js'],
14-
mode: ['async', 'sync'],
18+
mode: ['sync', 'promise', 'callback'],
1519
recursive: ['true', 'false'],
1620
};
1721

@@ -20,15 +24,33 @@ const bench = common.createBenchmark(main, configs);
2024
async function main(config) {
2125
const fullPath = path.resolve(benchmarkDirectory, config.dir);
2226
const { pattern, recursive, mode } = config;
27+
const options = { cwd: fullPath, recursive };
28+
const callback = (resolve, reject) => {
29+
glob(pattern, options, (err, matches) => {
30+
if (err) {
31+
reject(err);
32+
} else {
33+
resolve(matches);
34+
}
35+
});
36+
};
2337

2438
let noDead;
2539
bench.start();
2640

2741
for (let i = 0; i < config.n; i++) {
28-
if (mode === 'async') {
29-
noDead = await fs.promises.glob(pattern, { cwd: fullPath, recursive });
30-
} else {
31-
noDead = fs.globSync(pattern, { cwd: fullPath, recursive });
42+
switch (mode) {
43+
case 'sync':
44+
noDead = globSync(pattern, options);
45+
break;
46+
case 'promise':
47+
noDead = await globAsync(pattern, options);
48+
break;
49+
case 'callback':
50+
noDead = await new Promise(callback);
51+
break;
52+
default:
53+
throw new Error(`Unknown mode: ${mode}`);
3254
}
3355
}
3456

0 commit comments

Comments
 (0)