Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
set.add test2
(version: 0)
Comparing performance of:
set vs arr
Created:
2 years ago
by:
Guest
Jump to the latest result
Tests:
set
const set = new Set() let check = 0 for (let i = 0; i < 100000; i++) { const val = Math.random() set.add(val) set.has(val) }
arr
const arr = [] let check = 0 for (let i = 0; i < 100000; i++) { const val = Math.random() arr.push(val) if (i > 10) check++ arr.includes(arr[i]) }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
set
arr
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36
Browser/OS:
Chrome 131 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
set
106.2 Ops/sec
arr
0.5 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down what's being tested in the provided JSON benchmark. **What is being tested?** The two individual test cases are designed to measure the performance of adding elements to a data structure and checking if an element exists within it. In the first test case, `set.add test2`, we have: * A new Set object created. * 100,000 iterations where: + A random value is generated using `Math.random()`. + The value is added to the Set using `set.add(val)`. + The existence of the value in the Set is checked using `set.has(val)`. In the second test case, `arr`, we have: * An empty array created. * 100,000 iterations where: + A random value is generated using `Math.random()`. + The value is pushed onto the array using `arr.push(val)`. + If the iteration index is greater than 10, a counter variable `check` is incremented. + The existence of the element at the current index in the array is checked using `arr.includes(arr[i])`. **Options compared** The two test cases are comparing different approaches to adding elements to a data structure and checking for existence: 1. **Set**: Using a Set object, which provides fast membership testing (i.e., checking if an element exists). 2. **Array**: Using an array, where elements must be added at the end using `push()` or `unshift()`, and checking for existence using `includes()`. **Pros and Cons of each approach** **Set:** Pros: * Fast membership testing * Efficient insertion and removal operations Cons: * May not provide a complete order of elements (e.g., no guarantee that the elements are stored in any particular order) * Can be slower than arrays for certain use cases, especially when elements need to be inserted or removed frequently **Array:** Pros: * Elements can be added at any position using `push()` or `unshift()`. * Can provide a complete order of elements. Cons: * Membership testing (e.g., using `includes()`) can be slower than with Sets. * Insertion and removal operations can be slower than with Sets. **Other considerations** * Both test cases use `Math.random()` to generate random values, which may affect the results due to randomness in performance. * The counter variable `check` in the array test case is used only for testing purposes and does not affect the actual performance measurement. **Libraries and special JS features** In this benchmark, no specific library or syntax is being tested. However, note that Set objects are a built-in JavaScript data structure, and arrays are also native to JavaScript. If you'd like to explore alternative approaches using libraries or specialized techniques (e.g., using `Map` instead of `Set`), we can discuss those as well!
Related benchmarks:
set operations
Destructuring assignment vs Set iterator - V2
new Set vs set.clear()
incl/set
Comments
Confirm delete:
Do you really want to delete benchmark?