lukeed / uuid

A tiny (~230B)and fast UUID (V4) generator for Node and the browser

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

What about add support for crypto.randomUUID?

sergeysova opened this issue · comments

Sounds good, I'll post a benchmark here (trimmed):

> envinfo --system --browsers
OS: macOS 12.6
CPU: (8) x64 Intel(R) Core(TM) i5-8257U CPU @ 1.40GHz
Memory: 146.19 MB / 8.00 GB
Shell: 5.8.1 - /bin/zsh
Google Chrome: 105.0.5195.125 x86_64
Node.js: 18.9.0

> node index.js
  String.replace(Math.random)  x 327,566 ops/sec ±0.63% (96 runs sampled)
  String.replace(crypto)       x 11,442 ops/sec ±0.53% (88 runs sampled)
  uuid/v4                      x 1,062,243 ops/sec ±0.60% (91 runs sampled)
  @lukeed/uuid                 x 4,153,544 ops/sec ±0.33% (96 runs sampled)
  @lukeed/uuid/secure          x 4,279,066 ops/sec ±0.25% (95 runs sampled)
  crypto.randomUUID            x 4,171,723 ops/sec ±0.22% (97 runs sampled)

> nr build && w7 browser
(in browser console)
  String.replace(Math.random)  x 407,023 ops/sec ±0.63% (66 runs sampled)
  uuid/v4                      x 572,607 ops/sec ±1.09% (67 runs sampled)
  @lukeed/uuid                 x 3,557,690 ops/sec ±0.70% (65 runs sampled)
  @lukeed/uuid/secure          x 3,753,007 ops/sec ±1.65% (65 runs sampled)
  crypto.randomUUID            x 421,675 ops/sec ±11.75% (51 runs sampled)

So it turns out that using crypto.randomUUID does not gain much performance (especially in the browser).

It’s a slower version of this library. I remember the PR that added it to Node core.