Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
UUID V4 Crypto vs Math
(version: 0)
Crypto.getRandomValues vs Math.random
Comparing performance of:
math vs crypto vs crypto Number to String
Created:
5 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
function uuidCrypto() { let bytes = window.crypto.getRandomValues(new Uint8Array(32)); const randomBytes = () => (bytes = bytes.slice(1)) && bytes[0]; return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, c => (c ^ randomBytes() & 15 >> c / 4).toString(16)) } function uuidMath() { return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, c => { const r = (Math.random() * 16) | 0; const v = c === 'x' ? r : (r & 0x3) | 0x8; return v.toString(16); }); } function uuidCryptoNumber() { let bytes = window.crypto.getRandomValues(new Uint8Array(32)); const randomBytes = () => (bytes = bytes.slice(1)) && bytes[0]; return ([1e7] + -1e3 + -4e3 + -8e3 + -1e11).replace(/[018]/g, c => (c ^ randomBytes() & 15 >> c / 4).toString(16) ); }
Tests:
math
uuidMath()
crypto
uuidCrypto()
crypto Number to String
uuidCryptoNumber()
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
math
crypto
crypto Number to String
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/136.0.0.0 Mobile Safari/537.36
Browser/OS:
Chrome Mobile 136 on Android
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
math
744452.7 Ops/sec
crypto
164233.7 Ops/sec
crypto Number to String
180665.5 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**Benchmark Explanation** The provided JSON represents a JavaScript microbenchmark, specifically comparing the performance of three different approaches for generating Universally Unique Identifiers (UUIDs) in JavaScript: 1. **`uuidMath()`**: This function uses `Math.random()` to generate random numbers and then constructs a UUID string by replacing hexadecimal characters with values generated from `Math.random()`. 2. **`uuidCrypto()`**: This function uses the Web Cryptography API (`window.crypto`) to generate cryptographically secure random bytes, which are then sliced and shifted to create a UUID string. 3. **`uuidCryptoNumber()`**: This function generates a UUID string by replacing hexadecimal characters with values generated from `Math.random()`, similar to `uuidMath()`, but uses a specific format to match the required UUID structure. **Comparison of Options** The benchmark compares the performance of these three approaches: * **`uuidMath()`**: Simple and fast, but may not be suitable for cryptographic purposes due to the use of non-cryptographically secure random numbers. * **`uuidCrypto()`**: Provides cryptographically secure randomness, which is essential for generating unique IDs that can be used for security purposes. However, it may be slower than `uuidMath()` due to the overhead of Web Cryptography API operations. * **`uuidCryptoNumber()`**: A hybrid approach that combines the speed of `uuidMath()` with the cryptographic security of `uuidCrypto()`. It uses a specific format to match the required UUID structure. **Pros and Cons** | Approach | Pros | Cons | | --- | --- | --- | | `uuidMath()` | Fast, simple | Not suitable for cryptography | | `uuidCrypto()` | Cryptographically secure | Slower than `uuidMath()` | | `uuidCryptoNumber()` | Balances speed and security | May have performance overhead due to formatting | **Library and Purpose** The Web Cryptography API (`window.crypto`) is used in `uuidCrypto()`, which provides a way to generate cryptographically secure random numbers for use in cryptographic algorithms, such as generating UUIDs. **Special JS Features/Syntax** This benchmark does not specifically utilize any special JavaScript features or syntax beyond what is typically available in modern JavaScript engines. However, it relies on the Web Cryptography API, which may require specific configuration and setup to work correctly. **Alternatives** Other alternatives for generating UUIDs in JavaScript include: * Using a library like `uuid.js`, which provides a simple and efficient way to generate UUIDs. * Using a random number generator algorithm that is not part of the Web Cryptography API, such as the `crypto.randomBytes()` method on Node.js. Note that these alternatives may have different trade-offs in terms of performance, security, and simplicity compared to the approaches used in this benchmark.
Related benchmarks:
UUID Test 3
UUID Generator
Math.random vs crypto.getRandomValues vs uuid
Creating Uids v2
Comments
Confirm delete:
Do you really want to delete benchmark?