summaryrefslogtreecommitdiff
path: root/benchmark/fs/bench-opendir.js
blob: 419c3a231a850b5d2f294a513ef306afaca2863e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
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);
}