function generateUUIDs(n) {
const uuids = [];
for (let i = 0; i < n; i++) {
uuids.push(crypto.randomUUID());
}
return uuids;
}
// Example usage:
const numberOfUUIDs = 100000;
var uuids = generateUUIDs(numberOfUUIDs);
const set = new Set()
for (let index = 0; index < uuids.length; index++) {
const uuid = uuids[index]
set.add(uuid)
}
for (let index = 0; index < uuids.length; index++) {
const uuid = uuids[index]
set.add(uuid)
}
const set = new Set()
for (let index = 0; index < uuids.length; index++) {
set.add(index)
}
for (let index = 0; index < uuids.length; index++) {
set.add(index)
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
String Set | |
Index Set |
Test name | Executions per second |
---|---|
String Set | 27.2 Ops/sec |
Index Set | 75.6 Ops/sec |
Let's dive into the world of MeasureThat.net and explore what's being tested in this benchmark.
Benchmark Definition
The benchmark is designed to compare two approaches for adding elements to a Set
data structure in JavaScript: using strings and using numbers.
Script Preparation Code
The script preparation code generates an array of 100,000 random UUIDs (universally unique identifiers) using the crypto.randomUUID()
function. This will be used as input for both test cases.
Html Preparation Code
There is no HTML preparation code provided, which means that this benchmark is a headless test, only running in the JavaScript environment.
Individual Test Cases
There are two test cases:
add()
method.add()
method.Options Compared
The two options being compared are:
Pros and Cons
Here's a brief summary of the pros and cons of each approach:
String Set
Pros:
Cons:
Index Set
Pros:
Cons:
Library and Purpose
In both test cases, the Set
data structure is used, which is a built-in JavaScript object that provides an efficient way to store unique values.
Special JS Feature or Syntax
None mentioned in this benchmark. However, it's worth noting that the crypto.randomUUID()
function is available in modern browsers and Node.js environments.
Other Alternatives
If you were to implement this benchmark yourself, you could also consider using other data structures like:
Keep in mind that the performance differences between these alternatives may not be significant enough to matter for most use cases.