Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

uuid-random

jchook1mMIT1.3.2TypeScript support: included

Fastest UUIDv4 with good RNG

uuid, fast, random, v4, tiny

readme

uuid-random

MIT Licence Stable

Generate RFC-4122 compliant random UUIDs (version 4) with better statistical dispersion than Math.random().

Install

npm i uuid-random

Or download the latest release.

Features

Compatability

Compatible with almost all versions of:

  • Node
  • Babel
  • TypeScript
  • Web browsers

Performance

The included benchmark.js as well as independent benchmarks rank this library as the fastest pure JS UUID v4 generator available with cryptographically secure PRNG— almost 20x faster than the most popular library (using latest NodeJS).

npm package performance
portable-uuid 354k ops/sec
uuid 474k ops/sec
id128 6.0M ops/sec
uuid-random (this) 9.7M ops/sec

Results above generated on a 4.20GHz Intel i7-7700K with Node v12.18.0

Why use UUID?

U*niversally *Unique IDentifiers transcend many constraints of traditional incremental integer IDs, especially in distributed systems. In UUID version 4, we essentially generate a random 128-bit value.

We do trade 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).

Note, if you plan to use UUIDs for a new project, depending on your requirements, you may consider a more recent standard that addresses some of the shortcomings of UUID, such as flake-id, nanoid, cuid, or ulid.

Example Usage

Babel

import uuid from 'uuid-random';
uuid(); // 'f32dc9ae-7ca8-44ca-8f25-f258f7331c55'

Node

var uuid = require('uuid-random');
uuid(); // '0b99b82f-62cf-4275-88b3-de039020f14e'

Browser

<script src="uuid-random.min.js"></script>
<script>
  uuid(); // 'b96ab5e6-f1e8-4653-ab08-4dd82ea65778'
</script>

Validate a UUID v4 String

uuid.test('0b99b82f-62cf-4275-88b3-de039020f14e'); // true

Generate Binary UUIDs

uuid.bin(); // <Buffer 41 db 10 54 b3 61 48 50 87 f1 2f 7b 08 a5 0f 06>

Contributing

Feel free to open an issue or submit a pull request.

License

MIT.

changelog

Change log

The user-friendly summaries of how this project evolved over time.

1.3.2 - Jun 24, 2020

  • Fixes issue with test() case-sensitivity (#17)
  • Improves tests
  • Improves benchmarks

1.3.1 - Jun 19, 2020

  • Fixes issue with iOS 9 (#15)

1.3.0 - Sept 14, 2019

  • Exposes uuid.randomBytes() so you can override it, e.g. with nacl (#5)
  • Exposes uuid.clearBuffer() to clear the internal RNG buffer
  • Performance improvement via refactor (#5)
  • Fix Math.random() fallback range from [0,255) to [0,255] (#6)
  • Updated benchmarks (thanks @aarondcohen)
  • README, etc now written in E-Prime

Note: version jump to 1.3.0 since I should have named 1.0.4 ⇒ 1.1.0 and 1.0.5 ⇒ 1.2.0

1.0.9

  • Type definition for TypeScript

1.0.8

  • Fix for IE11 when using crypto library (check window.msCrypto)

1.0.7

  • Minor performance improvement
  • Benchmarks latest npm uuid packages
  • Updates code formatting and README
  • Still the fastest pure JS UUIDv4 generator x2

1.0.5

  • Fixes notice when environment does not support crypto
  • Exposes uuid.test() function to validate a uuid v4 string

1.0.4

  • Improves performance when generating large batches of uuids
  • Exposes uuid.bin() for generating binary uuids

1.0.3

  • Verified RFC-4122 compliance

1.0.2

  • Bug fix

1.0.1

  • Increased performance dramatically - now the fastest pure JS UUIDv4 generator x5

1.0.0

  • Initial release