diff --git a/benchmark/fs/bench-glob.js b/benchmark/fs/bench-glob.js new file mode 100644 index 00000000000..02ecf929269 --- /dev/null +++ b/benchmark/fs/bench-glob.js @@ -0,0 +1,37 @@ +'use strict'; + +const common = require('../common'); +const fs = require('fs'); +const path = require('path'); +const assert = require('node:assert'); + +const benchmarkDirectory = path.resolve(__dirname, '..', '..'); + +const configs = { + n: [1e3], + dir: ['lib'], + pattern: ['**/*', '*.js', '**/**.js'], + mode: ['async', 'sync'], + recursive: ['true', 'false'], +}; + +const bench = common.createBenchmark(main, configs); + +async function main(config) { + const fullPath = path.resolve(benchmarkDirectory, config.dir); + const { pattern, recursive, mode } = config; + + 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 }); + } + } + + bench.end(config.n); + assert.ok(noDead); +} diff --git a/lib/fs.js b/lib/fs.js index 53e4d5b829b..32bcbb4f417 100644 --- a/lib/fs.js +++ b/lib/fs.js @@ -25,6 +25,7 @@ 'use strict'; const { + ArrayFromAsync, ArrayPrototypePush, BigIntPrototypeToString, Boolean, @@ -3115,10 +3116,7 @@ function glob(pattern, options, callback) { // TODO: Use iterator helpers when available (async () => { try { - const res = []; - for await (const entry of new Glob(pattern, options).glob()) { - ArrayPrototypePush(res, entry); - } + const res = await ArrayFromAsync(new Glob(pattern, options).glob()); callback(null, res); } catch (err) { callback(err); diff --git a/typings/primordials.d.ts b/typings/primordials.d.ts index e53ea514a9b..8e0dd1c521b 100644 --- a/typings/primordials.d.ts +++ b/typings/primordials.d.ts @@ -111,6 +111,7 @@ declare namespace primordials { export const ArrayPrototype: typeof Array.prototype export const ArrayIsArray: typeof Array.isArray export const ArrayFrom: typeof Array.from + export const ArrayFromAsync: typeof Array.fromAsync export const ArrayOf: typeof Array.of export const ArrayPrototypeConcat: UncurryThis export const ArrayPrototypeCopyWithin: UncurryThis