summaryrefslogtreecommitdiff
path: root/benchmark/crypto/randomInt.js
blob: ac5286f1775e1c685cd7677168505854afa16180 (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
'use strict';

const common = require('../common.js');
const { randomInt } = require('crypto');

const bench = common.createBenchmark(main, {
  mode: ['sync', 'async-sequential', 'async-parallel'],
  min: [-(2 ** 47) + 1, -10_000, -100],
  max: [100, 10_000, 2 ** 47],
  n: [1e3, 1e5],
});

function main({ mode, min, max, n }) {
  if (mode === 'sync') {
    bench.start();
    for (let i = 0; i < n; i++)
      randomInt(min, max);
    bench.end(n);
  } else if (mode === 'async-sequential') {
    bench.start();
    (function next(i) {
      if (i === n)
        return bench.end(n);
      randomInt(min, max, () => {
        next(i + 1);
      });
    })(0);
  } else {
    bench.start();
    let done = 0;
    for (let i = 0; i < n; i++) {
      randomInt(min, max, () => {
        if (++done === n)
          bench.end(n);
      });
    }
  }
}