Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Set string vs number (100k)
(version: 0)
Compare Set with string and number
Comparing performance of:
String Set vs Index Set
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
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);
Tests:
String Set
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) }
Index Set
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) }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
String Set
Index Set
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36
Browser/OS:
Chrome 120 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
String Set
27.2 Ops/sec
Index Set
75.6 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
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: 1. **String Set**: This test case adds each UUID to the set using the `add()` method. 2. **Index Set**: This test case adds each index of the array (0-99,999) to the set using the `add()` method. **Options Compared** The two options being compared are: * Adding strings to a Set * Adding numbers to a Set **Pros and Cons** Here's a brief summary of the pros and cons of each approach: **String Set** Pros: * More intuitive and familiar for many developers, as it's similar to how you would add strings to an array. Cons: * May be slower due to the overhead of converting numbers to strings before adding them to the set. * Some sets may not work well with duplicate values (in this case, two identical UUIDs), although MeasureThat.net uses a hash-based implementation that handles duplicates efficiently. **Index Set** Pros: * Faster, as numbers can be added directly without conversion. * Less memory-intensive, as no additional string overhead is required. Cons: * May not be as intuitive or familiar for many developers, as it's less common to work with indices in sets. * Some sets may not work well with duplicate values (in this case, two identical indices). **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: * Arrays: You can use arrays to store unique values, but they would require manual management to avoid duplicates. * Maps: JavaScript objects (Maps) can be used to store key-value pairs, which could be used as an alternative to sets for storing unique strings or numbers. Keep in mind that the performance differences between these alternatives may not be significant enough to matter for most use cases.
Related benchmarks:
set.has vs. array.includes - 1000 UUIDs
UUID Generation compare
Set string vs number
Set string vs number #1
Comments
Confirm delete:
Do you really want to delete benchmark?