X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=random.mjs;h=37bac03bc3ff794aa03f34736a852d5b3785dca1;hb=HEAD;hp=16037df91dff6aaaa47880b1247be00525161878;hpb=4aa2893a787119ef9b3957e82539fee4fad4f98f;p=benchmarks-js.git diff --git a/random.mjs b/random.mjs index 16037df..e3bd25f 100644 --- a/random.mjs +++ b/random.mjs @@ -1,11 +1,13 @@ import { randomInt } from 'node:crypto' -import Benchmark from 'benny' + +import { bench, group, run } from 'tatami-ng' + import { secureRandom, - secureRandomWithRandomValues + secureRandomWithRandomValues, } from './benchmark-utils.mjs' -const maximum = 281474976710654 +const maximum = 281474976710655 /** * @param max @@ -17,7 +19,7 @@ function getSecureRandomInteger (max = Number.MAX_SAFE_INTEGER, min = 0) { throw new RangeError('Invalid interval') } max = Math.floor(max) - if (min != null && min !== 0) { + if (min !== 0) { min = Math.ceil(min) return Math.floor(secureRandom() * (max - min + 1)) + min } @@ -37,7 +39,7 @@ function getSecureRandomIntegerWithRandomValues ( throw new RangeError('Invalid interval') } max = Math.floor(max) - if (min != null && min !== 0) { + if (min !== 0) { min = Math.ceil(min) return Math.floor(secureRandomWithRandomValues() * (max - min + 1)) + min } @@ -54,50 +56,28 @@ function getRandomInteger (max = Number.MAX_SAFE_INTEGER, min = 0) { throw new RangeError('Invalid interval') } max = Math.floor(max) - if (min != null && min !== 0) { + if (min !== 0) { min = Math.ceil(min) return Math.floor(Math.random() * (max - min + 1)) + min } return Math.floor(Math.random() * (max + 1)) } -Benchmark.suite( - 'Random Integer Generator', - Benchmark.add('Secure random integer generator', () => { +group('Random Integer Generator', () => { + bench('Secure random integer generator', () => { getSecureRandomInteger(maximum) - }), - Benchmark.add( - 'Secure random with getRandomValues() integer generator', - () => { - getSecureRandomIntegerWithRandomValues(maximum) - } - ), - Benchmark.add('Crypto random integer generator', (max = maximum, min = 0) => { - max = Math.floor(max) - if (min != null && min !== 0) { - min = Math.ceil(min) - return Math.floor(randomInt(min, max + 1)) - } - return Math.floor(randomInt(max + 1)) - }), - Benchmark.add('Math random integer generator', () => { + }) + bench('Secure random with getRandomValues() integer generator', () => { + getSecureRandomIntegerWithRandomValues(maximum) + }) + bench('Crypto random integer generator', () => { + randomInt(maximum) + }) + bench('Math random integer generator', () => { getRandomInteger(maximum) - }), - Benchmark.cycle(), - Benchmark.complete(), - Benchmark.save({ - file: 'random-integer-generator', - format: 'json', - details: true - }), - Benchmark.save({ - file: 'random-integer-generator', - format: 'chart.html', - details: true - }), - Benchmark.save({ - file: 'random-integer-generator', - format: 'table.html', - details: true }) -).catch(console.error) +}) + +await run({ + units: true, +})