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)
);
}
uuidMath()
uuidCrypto()
uuidCryptoNumber()
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
math | |
crypto | |
crypto Number to String |
Test name | Executions per second |
---|---|
math | 1304991.0 Ops/sec |
crypto | 266247.5 Ops/sec |
crypto Number to String | 301806.4 Ops/sec |
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:
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()
.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.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:
uuid.js
, which provides a simple and efficient way to generate UUIDs.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.