function uuid() {
// Public Domain/MIT
var d = new Date().getTime(); //Timestamp
var d2 =
(typeof performance !== "undefined" &&
performance.now &&
performance.now() * 1000) ||
0; //Time in microseconds since page-load or 0 if unsupported
return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, function (c) {
var r = Math.random() * 16; //random number between 0 and 16
if (d > 0) {
//Use timestamp until depleted
r = (d + r) % 16 | 0;
d = Math.floor(d / 16);
} else {
//Use microseconds since page-load if supported
r = (d2 + r) % 16 | 0;
d2 = Math.floor(d2 / 16);
}
return (c === "x" ? r : (r & 0x3) | 0x8).toString(16);
});
}
function shortId() {
// I generate the UID from two parts here
// to ensure the random number provide enough bits.
let firstPart = (Math.random() * 46656) | 0;
let secondPart = (Math.random() * 46656) | 0;
firstPart = ("000" + firstPart.toString(36)).slice(-3);
secondPart = ("000" + secondPart.toString(36)).slice(-3);
return firstPart + secondPart;
}
function shuffle(array) {
let currentIndex = array.length,
randomIndex;
// While there remain elements to shuffle.
while (currentIndex != 0) {
// Pick a remaining element.
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex--;
// And swap it with the current element.
[array[currentIndex], array[randomIndex]] = [
array[randomIndex],
array[currentIndex],
];
}
return array;
}
const NB_ITEMS = 100000;
var objectWithVeryShortKeys = {};
var veryShortKeys = [];
for (let i = 0; i < NB_ITEMS; i++) {
let key = shortId();
objectWithVeryShortKeys[key] = Math.random();
veryShortKeys.push(key);
}
var objectWithShortKeys = {};
var shortKeys = [];
for (let i = 0; i < NB_ITEMS; i++) {
let key = uuid();
objectWithShortKeys[key] = Math.random();
shortKeys.push(key);
}
var objectWithMediumKeys = {};
var mediumKeys = [];
for (let i = 0; i < NB_ITEMS; i++) {
let key = `${uuid()}::${uuid()}`;
objectWithMediumKeys[key] = Math.random();
mediumKeys.push(key);
}
var objectWithLongKeys = {};
var longKeys = [];
for (let i = 0; i < NB_ITEMS; i++) {
let key = `${uuid()}::${uuid()}::some_more_text::${uuid()}`;
objectWithLongKeys[key] = Math.random();
longKeys.push(key);
}
var objectWithVeryLongKeys = {};
var veryLongKeys = [];
for (let i = 0; i < NB_ITEMS; i++) {
let key = `${uuid()}::${uuid()}::some_more_text::${uuid()}::${uuid()}::${uuid()}::some_more_text::${uuid()}`;
objectWithVeryLongKeys[key] = Math.random();
veryLongKeys.push(key);
}
shuffle(veryShortKeys);
shuffle(shortKeys);
shuffle(mediumKeys);
shuffle(longKeys);
shuffle(veryLongKeys);
let str = "";
for (let i = 0; i < 10000; i++) {
str += objectWithVeryShortKeys[veryShortKeys[i]];
}
let shortKeysStr = "";
for (let i = 0; i < 10000; i++) {
shortKeysStr += objectWithShortKeys[shortKeys[i]];
}
let mediumKeysStr = "";
for (let i = 0; i < 10000; i++) {
mediumKeysStr += objectWithMediumKeys[mediumKeys[i]];
}
let longKeysStr = "";
for (let i = 0; i < 10000; i++) {
longKeysStr += objectWithLongKeys[longKeys[i]];
}
let veryLongKeysStr = "";
for (let i = 0; i < 10000; i++) {
veryLongKeysStr += objectWithVeryLongKeys[veryLongKeys[i]];
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Very short keys | |
Short keys | |
Medium keys | |
Long keys | |
Very long keys |
Test name | Executions per second |
---|---|
Very short keys | 115.4 Ops/sec |
Short keys | 69.6 Ops/sec |
Medium keys | 198.9 Ops/sec |
Long keys | 212.6 Ops/sec |
Very long keys | 208.9 Ops/sec |
Based on the provided code snippets and benchmark results, I'll attempt to answer your question.
It appears that you're comparing the performance of different key length benchmarks using Firefox 111 on a Mac OS X 10.13 device. The benchmark definitions are as follows:
objectWithVeryShortKeys[veryShortKeys[i]]
10,000 times.objectWithShortKeys[shortKeys[i]]
10,000 times.objectWithMediumKeys[mediumKeys[i]]
10,000 times.objectWithLongKeys[longKeys[i]]
10,000 times.objectWithVeryLongKeys[veryLongKeys[i]]
10,000 times.The benchmark results show the following order of performance (fastest to slowest):
Note that the execution times are relatively close, indicating that the difference in performance is not dramatic.
Without more context or information about the specific use case or requirements of these benchmarks, it's difficult to provide a definitive answer. However, based on the provided data, Long keys seem to be the fastest, followed closely by Very long keys, suggesting that shorter key lengths result in better performance.