npm install uuid-random
- 0.3k minified + gzipped
- Uses better RNG when possible
- Works in browser or node with zero external dependencies
- Very fast!
What is UUID v4?
Universally Unique IDentifiers transcend many constraints of traditional incremental integer IDs, especially in distributed systems. With version 4, we (essentially) generate a random 128-bit value.
We do sacrifice guaranteed uniqueness for extremely probable uniqueness (you
would need to do-loop
uuid() at max speed for 73,067 years for a 50% chance of
one collision). But for that slight cost, we may now generate valid, unique,
persistent IDs on any node of a distributed system (e.g. intermittently offline
or high-latency clients).
import uuid from 'uuid-random'; uuid(); // 'f32dc9ae-7ca8-44ca-8f25-f258f7331c55'
var uuid = require('uuid-random'); uuid(); // '0b99b82f-62cf-4275-88b3-de039020f14e'
<script src="uuid-random.min.js"></script> <script> uuid(); // 'b96ab5e6-f1e8-4653-ab08-4dd82ea65778' </script>
uuid.test('0b99b82f-62cf-4275-88b3-de039020f14e'); // true
After digging through npm I settled on using node-uuid to take advantage of better RNG when possible. It's a great lib, but seemed too large and featured after using that neat oneliner-ish solution. So, I combined ideas from the better implementations and researched a much faster, more focused and compact uuid generator that used the best RNG available.
This library does one thing very well: generate UUID version 4.