@lukeed/uuid
A tiny (230B), fast, and cryptographically secure UUID (v4) generator for Node and the browser
Node.js
The Node.js module (235B) works in all versions of Node.js.
It is available in ESM and CommonJS formats, which means that both import
and require
syntax are supported.
Browser
The browser module (239B) works in all browsers with crypto.getRandomValues()
support.
It is available in UMD (under the uuid
global) and ESM formats. Any Rollup and webpack browser-configuration will select the correct file.
Install
$ npm install --save @lukeed/uuid
Usage
import uuid from '@lukeed/uuid';
uuid(); //=> '400fa120-5e9f-411e-94bd-2a23f6695704'
uuid(); //=> 'cd6ffb4d-2eda-4c84-aef5-71eb360ac8c5'
uuid(); //=> '9d20a138-56e1-481a-b8d5-dafdb79f3d2d'
API
uuid()
Returns: string
Creates a new Version 4 (random) RFC4122 UUID.
Benchmarks
Running on Node.js v10.13.0
Validation:
✔ String.replace(Math.random)
✔ String.replace(crypto)
✔ uuid/v4
✔ @lukeed/uuid
Benchmark:
String.replace(Math.random) x 408,120 ops/sec ±0.60% (94 runs sampled)
String.replace(crypto) x 13,888 ops/sec ±1.34% (87 runs sampled)
uuid/v4 x 327,221 ops/sec ±0.81% (90 runs sampled)
@lukeed/uuid x 6,069,155 ops/sec ±0.44% (93 runs sampled)
Performance
The reason why this UUID.V4 implementation is so much faster is two-fold:
- It composes an output with hexadecimal pairs (from a cached dictionary) instead of single characters.
- It allocates a larger Buffer/ArrayBuffer up front (expensive) and slices off chunks as needed (cheap).
The internal ArrayBuffer is 4096 bytes, which supplies 256 uuid()
invocations.
A larger buffer would result in higher performance over time, but I found this to be a good balance of performance and memory space.
License
MIT © Luke Edwards