Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Test UUID
(version: 0)
Comparing performance of:
1 vs 2 vs 3 vs 4 vs 5
Created:
3 years ago
by:
Registered User
Jump to the latest result
Tests:
1
const generateUUID = () => ([1e7] + -1e3 + -4e3 + -8e3 + -1e11).replace(/[018]/g, e => // eslint-disable-next-line no-bitwise (e ^ ((window.crypto || window.msCrypto).getRandomValues(new Uint8Array(1))[0] & (15 >> (e / 4)))).toString(16) ); generateUUID();
2
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();
3
function uuid1(a) { return a ? (a ^ ((Math.random() * 16) >> (a / 4))).toString(16) : ([1e7] + -1e3 + -4e3 + -8e3 + -1e11).replace(/[018]/g, uuid1); } uuid1()
4
var IDX=256, HEX=[], BUFFER; while (IDX--) HEX[IDX] = (IDX + 256).toString(16).substring(1); function v4() { var i=0, num, out=''; if (!BUFFER || ((IDX + 16) > 256)) { BUFFER = Array(i=256); while (i--) BUFFER[i] = 256 * Math.random() | 0; i = IDX = 0; } for (; i < 16; i++) { num = BUFFER[IDX + i]; if (i==6) out += HEX[num & 15 | 64]; else if (i==8) out += HEX[num & 63 | 128]; else out += HEX[num]; if (i & 1 && i > 1 && i < 11) out += '-'; } IDX++; return out; } v4();
5
crypto.randomUUID();
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (5)
Previous results
Fork
Test case name
Result
1
2
3
4
5
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):
Measuring the performance of different JavaScript UUID generation algorithms is a crucial task, especially in applications that require generating unique identifiers. The provided benchmark consists of five test cases: **1. `generateUUID` (Test Name: "1")** This function uses the `window.crypto` object to generate a random number between 0 and 15 (exclusive) and then converts it to hexadecimal using bitwise operations. The resulting UUID is then converted to a string. Pros: * Simple and straightforward implementation * Uses built-in crypto API for randomness Cons: * Performance might be slow due to the use of `window.crypto` * May not be suitable for cryptographic purposes due to the lack of true randomness **2. `uuid` (Test Name: "2")** This function generates a UUID by looping through 32 iterations, generating random numbers between 0 and 16, and then combining them to form a unique identifier. Pros: * Simple and easy to understand * Uses straightforward mathematical operations Cons: * Performance might be slower than other implementations due to the simplicity of the algorithm * May not be suitable for high-performance applications **3. `uuid1` (Test Name: "3")** This function is similar to `uuid`, but with a twist. It uses a recursive approach to generate the UUID, which can lead to improved performance. Pros: * Similar to `uuid`, but with potential performance benefits * Still relatively simple and easy to understand Cons: * Recursive approach might lead to stack overflow errors if not implemented correctly * Performance may vary depending on implementation details **4. `v4` (Test Name: "4")** This function generates a UUID using a more complex algorithm that involves generating random numbers from a buffer and then combining them to form a unique identifier. Pros: * More complex and potentially faster than other implementations due to the use of buffers * May be suitable for cryptographic purposes Cons: * Implementation is more complex and harder to understand * Performance may vary depending on implementation details **5. `crypto.randomUUID()` (Test Name: "5")** This function uses the `crypto` module in Node.js to generate a random UUID. Pros: * Uses built-in crypto API for randomness, making it suitable for cryptographic purposes * Potential performance benefits due to optimized implementation Cons: * Limited to Node.js environment, may not be suitable for browser-based applications * May have dependencies on other Node.js modules In general, the choice of algorithm depends on the specific requirements of your application. If you need a simple and easy-to-understand solution, `uuid` or `uuid1` might be sufficient. However, if you require high performance or cryptographic security, `v4` or `crypto.randomUUID()` might be more suitable. As for the provided benchmark results, it appears that `generateUUID` performs worst among all five test cases, while `crypto.randomUUID()` performs best. The other three test cases have relatively close execution times, indicating that they are well-balanced and suitable for most use cases.
Related benchmarks:
UUID perf test
Test uuidv1 vs uuidv4
Test UUID with different optimizations
Set string vs number
Set string vs number #1
Comments
Confirm delete:
Do you really want to delete benchmark?