function generateUUIDs(n) {
const uuids = [];
for (let i = 0; i < n; i++) {
uuids.push(crypto.randomUUID());
}
return uuids;
}
// Example usage:
const numberOfUUIDs = 10000;
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 | 459.7 Ops/sec |
Index Set | 888.2 Ops/sec |
Benchmark Overview
The provided JSON represents a JavaScript microbenchmark on MeasureThat.net, which compares the performance of two approaches: adding unique strings and indices to a Set data structure.
Script Preparation Code
The script preparation code is responsible for generating an array of 10,000 unique random UUIDs using the crypto.randomUUID()
function. This code is executed before each benchmark test case.
Html Preparation Code
There is no HTML preparation code provided, which means that the browser's default settings and rendering will be used for this benchmark.
Individual Test Cases
The two test cases are:
add()
method.add()
method.Library and Purpose
In both test cases, a Set data structure is used from the JavaScript standard library (Set
object). A Set is a collection of unique values, which allows for efficient lookups and additions.
Special JS Feature/Syntax
There are no special JavaScript features or syntaxes being tested in this benchmark. However, it's worth noting that the use of crypto.randomUUID()
to generate unique identifiers might be considered a modern JavaScript feature.
Pros and Cons of Approaches
Other Considerations
Alternative Benchmarks
If you'd like to explore alternative benchmarks, MeasureThat.net offers a wide range of microbenchmarking tests, including:
Keep in mind that each benchmark test case is designed to isolate specific performance characteristics or language features, so you may want to explore these alternatives to gain a deeper understanding of JavaScript's strengths and weaknesses.