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));
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))
([1e3]+-10+-10+-1e5).replace(/\d/g, () => crypto.getRandomValues(new Uint8Array(1))[0].toString(16).padStart(2,'0'))
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('');
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
uuidv4 | |
random 1 | |
random 2 | |
random 3 |
Test name | Executions per second |
---|---|
uuidv4 | 358682.9 Ops/sec |
random 1 | 129246.9 Ops/sec |
random 2 | 179074.1 Ops/sec |
random 3 | 569876.6 Ops/sec |
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:
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.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.random 2
: Similar to random 1
, but uses regular expressions to replace certain digits with the generated random hexadecimal value.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:
crypto.getRandomValues
with different approaches (UUID generation and random number generation)uuidv4
and random 3
)random 2
)Pros and Cons:
Here's a brief summary of each approach:
crypto.getRandomValues
: This function generates cryptographically secure random numbers, making it suitable for generating unique identifiers like UUIDs.uuidv4
and random 3
): This approach can lead to performance improvements by reducing the number of bytes that need to be generated or processed.random 2
): Using regular expressions can lead to slower performance compared to simple string concatenation or other methods.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:
uuid
(Node.js) or crypto uuid
(modern browsers).Keep in mind that these alternatives may not provide the same level of performance or security as the original approaches used in the benchmark.