fix: improve v4() performance by broofa · Pull Request #894 · uuidjs/uuid (original) (raw)

Just tested it, with the same configuration as #892 - and I do also observe the same improvement. v4() is now comparable to native. Great work! I got:

Starting. Tests take ~1 minute to run ...
uuid.stringify() x 6,568,232 ops/sec ±0.32% (99 runs sampled)
uuid.parse() x 4,711,958 ops/sec ±0.80% (98 runs sampled)
---

uuid.v1() x 5,137,524 ops/sec ±0.25% (97 runs sampled)
uuid.v1() fill existing array x 8,499,889 ops/sec ±0.27% (97 runs sampled)
uuid.v4() using crypto.randomUUID x 30,167,473 ops/sec ±1.44% (92 runs sampled)
uuid.v4() w/out crypto.randomUUID x 5,149,691 ops/sec ±0.20% (101 runs sampled)
uuid.v4() fill existing array x 10,593,187 ops/sec ±0.28% (100 runs sampled)
crypto.randomUUID() x 24,379,637 ops/sec ±1.32% (93 runs sampled)
uuid.v5() x 980,430 ops/sec ±0.25% (98 runs sampled)
uuid.v6() x 2,799,174 ops/sec ±0.17% (96 runs sampled)
uuid.v7() x 5,689,082 ops/sec ±0.16% (96 runs sampled)
uuid.v7() fill existing array x 9,301,364 ops/sec ±0.27% (98 runs sampled)
uuid.v7() with defined time x 5,853,892 ops/sec ±0.26% (96 runs sampled)
Fastest is uuid.v4() using crypto.randomUUID
---

uuid.v1ToV6() x 2,965,337 ops/sec ±0.40% (99 runs sampled)
uuid.v6ToV1() x 2,984,651 ops/sec ±0.20% (100 runs sampled)

30M ops/sec is what I observed with crypto.randomUUID directly in my own benchmark code. I'm not sure why that's only showing up as 24M in this benchmark suite, but that's neither here-nor-there.

How'd you come up with the refactor? A hunch about making the else-case look less desirable to the optimiser?