Last active
September 18, 2021 13:33
-
-
Save socheatsok78/3331cae1aea0e3d8841faea307c8128e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Get a random large number | |
*/ | |
export const getRandomNumber = () => parseInt((Math.random() + "").split('.')[1]) | |
/** | |
* Get random string identifier | |
*/ | |
export const getRandomString = () => getRandomNumber().toString(36) | |
/** | |
* Get a unique large random number | |
*/ | |
export const getUniqueRandomNumber = () => (getRandomNumber() + Date.now()) | |
/** | |
* Get unique random string identifier | |
*/ | |
export const getUniqueRandomString = () => getUniqueRandomNumber().toString(36) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const sleep = (timeout = 0) => new Promise(resolve => setTimeout(() => resolve(), timeout)) | |
async function main() { | |
const cached = new Map() | |
while (true) { | |
const val = getUniqueRandomString() | |
if (cached.has(val)) { | |
console.warn('Found duplicated value', { size: cached.size, value: val }) | |
break | |
} | |
cached.set(val) | |
console.log({ size: cached.size, value: val, length: val.length}) | |
await sleep(5) | |
} | |
} | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment