Tobias Nießen | 5dae7d6 | 2020-09-08 22:19:02 | [diff] [blame^] | 1 | 'use strict'; |
| 2 | |
| 3 | const common = require('../common.js'); |
| 4 | const { randomInt } = require('crypto'); |
| 5 | |
| 6 | const bench = common.createBenchmark(main, { |
| 7 | mode: ['sync', 'async-sequential', 'async-parallel'], |
| 8 | min: [-(2 ** 47) + 1, -10_000, -100], |
| 9 | max: [100, 10_000, 2 ** 47], |
| 10 | n: [1e3, 1e5] |
| 11 | }); |
| 12 | |
| 13 | function main({ mode, min, max, n }) { |
| 14 | if (mode === 'sync') { |
| 15 | bench.start(); |
| 16 | for (let i = 0; i < n; i++) |
| 17 | randomInt(min, max); |
| 18 | bench.end(n); |
| 19 | } else if (mode === 'async-sequential') { |
| 20 | bench.start(); |
| 21 | (function next(i) { |
| 22 | if (i === n) |
| 23 | return bench.end(n); |
| 24 | randomInt(min, max, () => { |
| 25 | next(i + 1); |
| 26 | }); |
| 27 | })(0); |
| 28 | } else { |
| 29 | bench.start(); |
| 30 | let done = 0; |
| 31 | for (let i = 0; i < n; i++) { |
| 32 | randomInt(min, max, () => { |
| 33 | if (++done === n) |
| 34 | bench.end(n); |
| 35 | }); |
| 36 | } |
| 37 | } |
| 38 | } |