From 598548d97b15d6a74375a7c24f214b98c71f37a9 Mon Sep 17 00:00:00 2001 From: Jonatan Nilsson Date: Sat, 7 Oct 2023 11:49:41 +0000 Subject: [PATCH] random generator benchmark --- benchmark/random.js | 46 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 benchmark/random.js diff --git a/benchmark/random.js b/benchmark/random.js new file mode 100644 index 0000000..3739b17 --- /dev/null +++ b/benchmark/random.js @@ -0,0 +1,46 @@ +import crypto from 'crypto' +import Benchmark from 'benchmarkjs-pretty' + +function TestGenerateRandomString() { + + return new Benchmark.default('test different method to generate random string)') + .add('crypto.randomBytes(16)', function() { + for (let i = 0; i < 25; i++) { + crypto.randomBytes(16).toString('base64') + } + }) + .add('crypto.randomBytes(32)', function() { + for (let i = 0; i < 25; i++) { + crypto.randomBytes(32).toString('base64') + } + }) + .add('random (11 characters long)', function() { + for (let i = 0; i < 25; i++) { + let out = Math.random().toString(36).substring(2, 14) + } + }) + .add('random (22 characters long)', function() { + for (let i = 0; i < 25; i++) { + let out = Math.random().toString(36).substring(2, 24) + + Math.random().toString(36).substring(2, 24) + } + }) + .add('random (44 characters long)', function() { + for (let i = 0; i < 25; i++) { + let out = Math.random().toString(36).substring(2, 24) + + Math.random().toString(36).substring(2, 24) + + Math.random().toString(36).substring(2, 24) + + Math.random().toString(36).substring(2, 24) + } + }) + .run() + .then(function() {}, function(e) { + console.error('error:', e) + process.exit(1) + }) +} + +TestGenerateRandomString() +.then(function() { + process.exit(0) +}) \ No newline at end of file