Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
uuid generation with crypto.getRandomValues
(version: 0)
Comparing performance of:
uuidv4 vs random 1 vs random 2 vs random 3
Created:
5 years ago
by:
Guest
Jump to the latest result
Tests:
uuidv4
bits = crypto.getRandomValues(new Uint8Array(20)) bits[5] = (bits[5] & 0x4F) | 0x40; bits[5] &= 0x4F; bits[5] |= 0x40; bits[8] = (bits[8] & 0xBF) | 0x80; bits = [...bits].map(e => e.toString(16).padStart(2,'0') ).reduce((a,c,i)=>a+([4,7,10,13].includes(i)?'-':c));
random 1
Array(20).fill(0).map(()=>crypto.getRandomValues(new Uint8Array(1))[0].toString(16).padStart(2,'0')).reduce((a,c,i)=>a+([4,7,10,13].includes(i)?'-':c))
random 2
([1e3]+-10+-10+-1e5).replace(/\d/g, () => crypto.getRandomValues(new Uint8Array(1))[0].toString(16).padStart(2,'0'))
random 3
let s=[]; crypto.getRandomValues(new Uint8Array(20)).map( e => s.push(e.toString(16).padStart(2,'0')) ); [4,7,10,13].map( idx => s[idx]='-' ); s.join('');
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
uuidv4
random 1
random 2
random 3
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and its test cases. **What is being tested?** MeasureThat.net is testing the performance of generating UUIDs (Universally Unique Identifiers) using different approaches in JavaScript. **Test cases:** The benchmark has four test cases: 1. `uuidv4`: Tests generating a UUID using the `crypto.getRandomValues` function, which returns an array of 16 bytes. The test modifies the second byte by shifting bits to create a new UUID. 2. `random 1`: Tests generating a single random number using `crypto.getRandomValues`, converting it to a hexadecimal string, and then concatenating it with other random numbers. 3. `random 2`: Similar to `random 1`, but uses regular expressions to replace certain digits with the generated random hexadecimal value. 4. `random 3`: Tests generating an array of 20 random hexadecimal values, joining them into a single string, and then modifying specific indices to create a new UUID. **Options being compared:** The benchmark is comparing the performance of these four test cases: * Using `crypto.getRandomValues` with different approaches (UUID generation and random number generation) * The impact of modification on the generated UUIDs (in `uuidv4` and `random 3`) * The use of regular expressions in string concatenation (in `random 2`) **Pros and Cons:** Here's a brief summary of each approach: 1. **`crypto.getRandomValues`**: This function generates cryptographically secure random numbers, making it suitable for generating unique identifiers like UUIDs. * Pros: Fast, secure, and widely supported. * Cons: May not be as efficient as other methods for generating simple random values. 2. **Modifying the generated value** (as in `uuidv4` and `random 3`): This approach can lead to performance improvements by reducing the number of bytes that need to be generated or processed. * Pros: Can improve performance, especially when working with large datasets. * Cons: May introduce additional complexity and potential errors due to the modification process. 3. **Regular expressions** (as in `random 2`): Using regular expressions can lead to slower performance compared to simple string concatenation or other methods. * Pros: Allows for more complex pattern matching and string manipulation. * Cons: Can introduce overhead and slow down the benchmark. **Libraries and features used:** None of the test cases use any external libraries. However, `crypto.getRandomValues` is a built-in function in Node.js and modern browsers. **Special JS feature or syntax:** The only special syntax used is the template literals (`\r\n` and `\n`) for string concatenation. **Alternatives:** If you were to rewrite these test cases using alternative approaches, here are some options: 1. Use a UUID library like `uuid` (Node.js) or `crypto uuid` (modern browsers). 2. Implement the random number generation using a different algorithm, such as the Linear Congruential Generator. 3. Use a faster string concatenation method, like using a StringBuilder or a library optimized for string manipulation. Keep in mind that these alternatives may not provide the same level of performance or security as the original approaches used in the benchmark.
Related benchmarks:
UUID Test 3
UUID V4 Crypto vs Math
uuid generation crypto.getRandomValues
Math.random vs crypto.getRandomValues vs uuid
Set string vs number
Comments
Confirm delete:
Do you really want to delete benchmark?