Skip to content

Instantly share code, notes, and snippets.

@mkuchak
Last active April 21, 2022 13:36
Show Gist options
  • Save mkuchak/62416bd449268fa6a72573027e962de3 to your computer and use it in GitHub Desktop.
Save mkuchak/62416bd449268fa6a72573027e962de3 to your computer and use it in GitHub Desktop.
Measure performance of NanoID and Web Crypto API to generate unique identifiers
// run it on browser console
const { nanoid } = await import(
'https://cdnjs.cloudflare.com/ajax/libs/nanoid/3.3.2/nanoid.min.js'
)
const loops = 1_000_000
console.time('NanoID')
for (let i = 0; i < loops; i++) {
nanoid()
}
console.timeEnd('NanoID')
console.time('WebCryptoUUID')
for (let i = 0; i < loops; i++) {
crypto.randomUUID()
}
console.timeEnd('WebCryptoUUID')
// Typical result:
// NanoID: 2849.41015625 ms
// WebCryptoUUID: 2215.301025390625 ms
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment