diff --git a/benchmark/fs/bench-opendir.js b/benchmark/fs/bench-opendir.js new file mode 100644 index 00000000000000..419c3a231a850b --- /dev/null +++ b/benchmark/fs/bench-opendir.js @@ -0,0 +1,51 @@ +'use strict'; + +const common = require('../common'); +const fs = require('fs'); +const path = require('path'); + +const bench = common.createBenchmark(main, { + n: [100], + dir: [ 'lib', 'test/parallel'], + mode: [ 'async', 'sync', 'callback' ] +}); + +async function main({ n, dir, mode }) { + const fullPath = path.resolve(__dirname, '../../', dir); + + bench.start(); + + let counter = 0; + for (let i = 0; i < n; i++) { + if (mode === 'async') { + // eslint-disable-next-line no-unused-vars + for await (const entry of await fs.promises.opendir(fullPath)) + counter++; + } else if (mode === 'callback') { + const dir = await fs.promises.opendir(fullPath); + await new Promise((resolve, reject) => { + function read() { + dir.read((err, entry) => { + if (err) { + reject(err); + } else if (entry === null) { + resolve(dir.close()); + } else { + counter++; + read(); + } + }); + } + + read(); + }); + } else { + const dir = fs.opendirSync(fullPath); + while (dir.readSync() !== null) + counter++; + dir.closeSync(); + } + } + + bench.end(counter); +} diff --git a/doc/api/fs.md b/doc/api/fs.md index d7a4ebcc2e8604..86e76f46f579af 100644 --- a/doc/api/fs.md +++ b/doc/api/fs.md @@ -362,8 +362,10 @@ Asynchronously read the next directory entry via readdir(3) as an After the read is completed, a `Promise` is returned that will be resolved with an [`fs.Dirent`][], or `null` if there are no more directory entries to read. -_Directory entries returned by this function are in no particular order as -provided by the operating system's underlying directory mechanisms._ +Directory entries returned by this function are in no particular order as +provided by the operating system's underlying directory mechanisms. +Entries added or removed while iterating over the directory may or may not be +included in the iteration results. ### dir.read(callback)