function oldmeth() {
function chr4 () {
return Math.random().toString(16).slice(-4);
}
return `${chr4() + chr4()}-${chr4()}-${chr4()}-${chr4()}-${chr4()}${chr4()}${chr4()}`;
}
function oldmethcrypto() {
function getRandom () {
return crypto.getRandomValues(new Uint32Array(4)).reduce((prev, current) => `${prev.toString(16)}${current.toString(16)}`);
}
function update4(uuid) {
const fourIndx = 12;
return `${uuid.slice(0,12)}4${uuid.slice(13)}`;
}
function getEight(c) {
return(c ^ crypto.getRandomValues(new Uint8Array(1))[0] & 15 >> c / 4).toString(16);
}
function update8(uuid) {
const eightIndx = 16;
return `${uuid.slice(0,16)}${getEight(8)}${uuid.slice(17)}`;
}
function addDashes(uuid) {
// xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx
return `${uuid.slice(0,8)}-${uuid.slice(8,12)}-${uuid.slice(12,16)}-${uuid.slice(16,20)}-${uuid.slice(20)}`;
}
let uuid = getRandom();
uuid = update4(uuid);
uuid = update8(uuid);
uuid = addDashes(uuid);
return uuid;
}
function oldmethcrypto2() {
function getRandom () {
return crypto.getRandomValues(new Uint32Array(4)).reduce((prev, current) => `${prev.toString(16)}${current.toString(16)}`);
}
function getEight(c) {
return(c ^ crypto.getRandomValues(new Uint8Array(1))[0] & 15 >> c / 4).toString(16);
}
let uuid = getRandom();
return `${uuid.slice(0,8)}-${uuid.slice(8,12)}-4${uuid.slice(13,16)}-${getEight(8)}${uuid.slice(17,20)}-${uuid.slice(20)}`;
}
function newmethrand1() {
function getRandom (num) {
let output;
for(let i = 0; i < Math.ceil(num/8); i++) {
output = `${output}${Math.random().toString(16).slice(-8)}`;
}
return output.slice(num * -1);
}
function getEight(c) {
return(c ^ Math.random().toString(16).slice(-1) & 15 >> c / 4).toString(16);
}
let uuid = getRandom(32);
return `${uuid.slice(0,8)}-${uuid.slice(8,12)}-4${uuid.slice(13,16)}-${getEight(8)}${uuid.slice(17,20)}-${uuid.slice(20)}`;
}
function newmeth() {
return ([1e7] + -1e3 + -4e3 + -8e3 + -1e11).replace(/[018]/g, c =>
(c ^ crypto.getRandomValues(new Uint8Array(1))[0] & 15 >> c / 4).toString(16),
);
}
oldmeth();
newmeth();
oldmethcrypto();
oldmethcrypto2();
newmethrand1();
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
old old | |
new | |
old crypt | |
old crypt 2 | |
old meth2 with random |
Test name | Executions per second |
---|---|
old old | 106844.4 Ops/sec |
new | 15775.3 Ops/sec |
old crypt | 111633.8 Ops/sec |
old crypt 2 | 145212.6 Ops/sec |
old meth2 with random | 113476.1 Ops/sec |
Measuring the performance of JavaScript code is crucial for optimizing and comparing different implementations.
Benchmark Definition
The provided JSON represents a benchmark definition, which outlines the characteristics of the test case:
oldmeth
: This function uses the hexadecimal representation of random numbers to create a UUID.oldmethcrypto
: This function uses the crypto module to generate a UUID. It first generates four random 32-bit integers, concatenates them, and then updates and adds dashes to create the final UUID.oldmethcrypto2
: Similar to oldmethcrypto
, but with some differences in the update and add-dashes logic.newmethrand1
and newmeth
: These functions use a custom implementation of UUID generation using random numbers.Individual Test Cases
The individual test cases are defined as follows:
old old
: This test case uses the oldmeth
function to generate a UUID.new
: This test case uses the newmeth
function to generate a UUID.old crypt
and old crypt 2
: These test cases use the oldmethcrypto
and oldmethcrypto2
functions, respectively, to generate UUIDs.old meth2 with random
: This test case uses the newmethrand1
function to generate a UUID.Latest Benchmark Result
The latest benchmark result shows the performance of each test case on Firefox 61 running on Windows:
Test Name | Executions Per Second |
---|---|
old crypt 2 | 113476.0625 |
old meth2 with random | 106844.390625 |
old crypt | 111633.7578125 |
old old | 15775.28125 |
new | 145212.59375 |
Analysis and Observations
Based on the benchmark results, we can observe:
oldmeth
function is significantly slower than the other implementations.newmeth
function performs better than the oldmethcrypto2
function.old meth2 with random
function has a performance similar to the newmeth
function.These observations suggest that the custom implementation of UUID generation in newmethrand1
and newmeth
is more efficient than the crypto-based implementations.