bits = crypto.getRandomValues(new Uint8Array(20))
bits[5] = (bits[5] & 0x4F) | 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=s.join('');
bits = crypto.getRandomValues(new Uint8Array(20))
bits[5] = (bits[5] & 0x4F) | 0x40;
bits[8] = (bits[8] & 0xBF) | 0x80;
bits = [bits].map(e => e.toString(16).padStart(2,'0') );
[4,7,10,13].map( idx => bits[idx]='-' ); bits = bits.join('');
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
uuidv4 | |
random 1 | |
random 2 | |
random 3 | |
uuidv4 with join |
Test name | Executions per second |
---|---|
uuidv4 | 246018.0 Ops/sec |
random 1 | 66309.4 Ops/sec |
random 2 | 83866.8 Ops/sec |
random 3 | 345411.0 Ops/sec |
uuidv4 with join | 194023.3 Ops/sec |
Let's break down the benchmark and its test cases.
Benchmark Overview
The benchmark is designed to measure the performance of generating UUIDs (Universally Unique Identifiers) using JavaScript's crypto
module. The tests aim to simulate real-world scenarios where UUIDs are generated and manipulated.
Test Cases
There are four test cases:
crypto.getRandomValues
and then joins the resulting hexadecimal values together.random 1
, but instead of replacing integers with their hexadecimal equivalents, it directly uses the resulting hexadecimal strings.crypto.getRandomValues
and then joins the resulting hexadecimal values together, just like in uuidv4
.Library: crypto
The crypto
module is a built-in JavaScript library that provides functions for cryptographic operations, including generating random numbers. In this benchmark, it's used to generate random UUIDs.
Special JS Features/Syntax
None of the test cases use any special JavaScript features or syntax beyond what's standard in modern JavaScript.
Approaches Compared
The four test cases compare different approaches to generating and manipulating UUIDs:
uuidv4
and uuidv4 with join
use direct concatenation of hexadecimal values to generate the final UUID.random 1
and random 2
use array manipulation techniques (e.g., map
, replace
) to process the generated random numbers.Pros and Cons
Here's a brief summary of the pros and cons of each approach:
Other Alternatives
If you were to rewrite these test cases using a different approach, some alternatives could include:
uuid
or uuid-lite
, which provide more efficient and standardized UUID generation mechanisms.However, given the simplicity of the benchmark and its focus on comparing different approaches, it's unlikely that significant changes would be made to alter the underlying approach.