function uuid() {
let uuid = "",
i,
random;
for (i = 0; i < 32; i++) {
random = (Math.random() * 16) | 0;
if (i == 8 || i == 12 || i == 16 || i == 20) {
uuid += "-";
}
uuid += (i == 12 ? 4 : i == 16 ? (random & 3) | 8 : random).toString(16);
}
return uuid;
}
uuid();
const ui8a = new Uint8Array(1);
function next() {
let uuid = "", i;
for (i = 0; i < 32; i++) {
uuid += (i === 8 || i === 13 || i === 18 || i === 23) ? '-' : (crypto.getRandomValues(ui8a)[0] * 16) | 0;
}
return uuid;
}
const ui8a = new Uint8Array(1);
function next() {
let uuid = [], i;
for (i = 0; i < 32; i++) {
uuid.push((i === 8 || i === 13 || i === 18 || i === 23) ? '-' : (crypto.getRandomValues(ui8a)[0] * 16) | 0);
}
return uuid.join('');
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
1 | |
2 | |
3 |
Test name | Executions per second |
---|---|
1 | 453142.0 Ops/sec |
2 | 11848811.0 Ops/sec |
3 | 11686905.0 Ops/sec |
Let's dive into the world of JavaScript microbenchmarks on MeasureThat.net.
Benchmark Overview
The provided benchmark measures the performance of three different approaches to generate UUIDs (Universally Unique Identifiers). A UUID is a 128-bit number used to identify information in computer systems. The benchmark definition specifies that each approach generates a random UUID with a specific format.
Approaches Compared
uuid()
function: This function uses the Math.random()
function to generate a random number between 0 and 16, and then performs bitwise operations to create the UUID.Uint8Array
with crypto.getRandomValues()
: This approach uses a Uint8Array
to generate a random byte and then converts it to an integer using bitwise operations. The crypto.getRandomValues()
function is used to generate cryptographically secure random numbers.Uint8Array
with push()
method: This approach also uses a Uint8Array
but pushes individual bytes onto the array and then joins them together using the join()
method.Pros and Cons of Each Approach
uuid()
function)`:Math.random()
.Uint8Array
with crypto.getRandomValues()
):crypto.getRandomValues()
.crypto
module, which may not be available in all environments.Uint8Array
with push()
method):join()
.Library Used
The crypto
library is used in Approach 2, which provides cryptographically secure random number generation. This library is part of the Node.js standard library, but it may not be available in all environments.
Other Considerations
crypto
module is available and configured correctly.Uint8Array
approach requires careful handling of integer overflows when converting bytes to integers.push()
method approach can lead to slower performance due to repeated array resizing.Alternatives
If you need to generate UUIDs in a browser environment, you may want to consider using the uuidv4()
function from the uuid
library, which provides a fast and secure way to generate UUIDs. However, keep in mind that this approach requires including an additional library in your project.
For server-side environments, you can use the crypto.randomBytes()
function to generate random bytes, which can then be converted to a UUID using bitwise operations.