uuid-random
Generate RFC-4122 compliant
random UUIDs
with better
statistical dispersion
than Math.random()
.
Install
npm install uuid-random
Features
- 0.3k minified + gzipped
- Uses better RNG when possible
- Works in browser or node with zero external dependencies
- Very fast!
Performance
This is the fastest pure javascript UUID v4 generator I have found,
over 5x faster than comparable libraries. After inspecting and/or testing every JS UUID package I could find (there are a ton), these were the fastest:
npm package | performance |
---|
node-uuid | 370k ops/sec |
portable-uuid | 260k ops/sec |
uuid | 370k ops/sec |
uuid-random | 2M ops/sec |
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).
Example Usage
Babel
import uuid from 'uuid-random';
uuid();
Node
var uuid = require('uuid-random');
uuid();
Browser
<script src="uuid-random.min.js"></script>
<script>
uuid();
</script>
Is UUID?
uuid.test('0b99b82f-62cf-4275-88b3-de039020f14e');
Rationale
Random (v4) UUIDs are often
better than
clock-based (v1), but Math.random()
sucks
for
uuid generation.
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.
License
MIT