Skip to content

Instantly share code, notes, and snippets.

@faultable faultable/uid.js
Created Sep 23, 2019

Embed
What would you like to do?
generate {n} random string using `randomBytes` from `crypto`
const { randomBytes } = require('crypto')
const totalGenerated = 31337
const endResults = []
function generateRandomBytes() {
console.time('randomBytes')
const randomString = randomBytes(6).toString('hex')
console.log('string:', randomString)
console.timeEnd('randomBytes')
return randomString
}
console.time('> overall')
// test mass generation
for (let i = 0; i < totalGenerated; i++) {
console.time('regression')
const generate = generateRandomBytes()
console.timeEnd('regression')
endResults.push(generate)
console.log()
}
function checkDuplicate(array) {
let total = []
for (var i = 0; i < array.length; i++) {
for (var j = i + 1; j < array.length; j++) {
if (array[i] === array[j]) {
total.push(array[i])
}
}
}
return total
}
const _checkDuplicate = checkDuplicate(endResults)
// stats
console.timeEnd('> overall')
console.log('> total random string:', totalGenerated)
console.log('> total duplicate string:', _checkDuplicate.length)
console.log('>', _checkDuplicate)
console.log()
@faultable

This comment has been minimized.

Copy link
Owner Author

faultable commented Sep 23, 2019

Macbook Air 1,6 GHz Intel Core i5, 8 GB 1600 MHz DDR3.

$ time node uid.js
...

string: ecb40f701f68
randomBytes: 0.026ms
regression: 0.044ms

> overall: 8493.303ms
> total random string: 31337
> total duplicate string: 0
> []

node uid.js  7.64s user 0.40s system 92% cpu 8.729 total

Node v10.16.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.