| 1 | const Benchmark = require('benny') |
| 2 | const { |
| 3 | secureRandom, |
| 4 | secureRandomWithRandomValues |
| 5 | } = require('./benchmark-utils') |
| 6 | const crypto = require('crypto') |
| 7 | |
| 8 | const maximum = 281474976710654 |
| 9 | |
| 10 | /** |
| 11 | * @param max |
| 12 | * @param min |
| 13 | * @returns |
| 14 | */ |
| 15 | function getSecureRandomInteger (max = Number.MAX_SAFE_INTEGER, min = 0) { |
| 16 | if (max < min || max < 0 || min < 0) { |
| 17 | throw new RangeError('Invalid interval') |
| 18 | } |
| 19 | max = Math.floor(max) |
| 20 | if (min != null && min !== 0) { |
| 21 | min = Math.ceil(min) |
| 22 | return Math.floor(secureRandom() * (max - min + 1)) + min |
| 23 | } |
| 24 | return Math.floor(secureRandom() * (max + 1)) |
| 25 | } |
| 26 | |
| 27 | /** |
| 28 | * @param max |
| 29 | * @param min |
| 30 | * @returns |
| 31 | */ |
| 32 | function getSecureRandomIntegerWithRandomValues ( |
| 33 | max = Number.MAX_SAFE_INTEGER, |
| 34 | min = 0 |
| 35 | ) { |
| 36 | if (max < min || max < 0 || min < 0) { |
| 37 | throw new RangeError('Invalid interval') |
| 38 | } |
| 39 | max = Math.floor(max) |
| 40 | if (min != null && min !== 0) { |
| 41 | min = Math.ceil(min) |
| 42 | return Math.floor(secureRandomWithRandomValues() * (max - min + 1)) + min |
| 43 | } |
| 44 | return Math.floor(secureRandomWithRandomValues() * (max + 1)) |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * @param max |
| 49 | * @param min |
| 50 | * @returns |
| 51 | */ |
| 52 | function getRandomInteger (max = Number.MAX_SAFE_INTEGER, min = 0) { |
| 53 | if (max < min || max < 0 || min < 0) { |
| 54 | throw new RangeError('Invalid interval') |
| 55 | } |
| 56 | max = Math.floor(max) |
| 57 | if (min != null && min !== 0) { |
| 58 | min = Math.ceil(min) |
| 59 | return Math.floor(Math.random() * (max - min + 1)) + min |
| 60 | } |
| 61 | return Math.floor(Math.random() * (max + 1)) |
| 62 | } |
| 63 | |
| 64 | Benchmark.suite( |
| 65 | 'Random Integer Generator', |
| 66 | Benchmark.add('Secure random integer generator', () => { |
| 67 | getSecureRandomInteger(maximum) |
| 68 | }), |
| 69 | Benchmark.add( |
| 70 | 'Secure random with getRandomValues() integer generator', |
| 71 | () => { |
| 72 | getSecureRandomIntegerWithRandomValues(maximum) |
| 73 | } |
| 74 | ), |
| 75 | Benchmark.add('Crypto random integer generator', (max = maximum, min = 0) => { |
| 76 | max = Math.floor(max) |
| 77 | if (min !== undefined && min !== 0) { |
| 78 | min = Math.ceil(min) |
| 79 | return Math.floor(crypto.randomInt(min, max + 1)) |
| 80 | } |
| 81 | return Math.floor(crypto.randomInt(max + 1)) |
| 82 | }), |
| 83 | Benchmark.add('Math random integer generator', () => { |
| 84 | getRandomInteger(maximum) |
| 85 | }), |
| 86 | Benchmark.cycle(), |
| 87 | Benchmark.complete(), |
| 88 | Benchmark.save({ |
| 89 | file: 'random-integer-generator', |
| 90 | format: 'json', |
| 91 | details: true |
| 92 | }), |
| 93 | Benchmark.save({ |
| 94 | file: 'random-integer-generator', |
| 95 | format: 'chart.html', |
| 96 | details: true |
| 97 | }), |
| 98 | Benchmark.save({ |
| 99 | file: 'random-integer-generator', |
| 100 | format: 'table.html', |
| 101 | details: true |
| 102 | }) |
| 103 | ) |