Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
set vs array
(version: 0)
Comparing performance of:
array vs set
Created:
5 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; var b = new Set(a)
Tests:
array
a.includes(10)
set
b.has(10)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
array
set
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36
Browser/OS:
Chrome 131 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
array
82069936.0 Ops/sec
set
143665728.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.1:latest
, generated one year ago):
Let's dive into the provided JSON data and explore what is being tested. **Benchmark Definition** The benchmark definition is a simple comparison between two approaches: using an array (`a`) vs using a Set (`b`) to check if an element exists in a collection of numbers. The preparation code creates an array `a` with 10 elements, and then creates a Set `b` from the same array. **Test Cases** There are two test cases: 1. **Array**: This test case uses the `includes()` method on the array `a` to check if the number `10` exists in the array. 2. **Set**: This test case uses the `has()` method on the Set `b` to check if the number `10` exists in the set. **Benchmark Results** The latest benchmark results show that both approaches are executed approximately 19 million times per second on a Chrome browser running on Windows Desktop. The results indicate that there is no significant performance difference between using an array and a Set for this specific use case. **What's being compared?** In this benchmark, we're comparing the execution speed of two approaches to check if an element exists in a collection of numbers: * Using an array (`a`) with the `includes()` method * Using a Set (`b`) with the `has()` method **Pros and Cons** Here are some pros and cons of using arrays vs Sets for this use case: **Arrays:** Pros: * Arrays are more familiar to developers, making it easier to understand and implement. * Arrays can store duplicate values (although this may not be desirable in some cases). Cons: * Array lookups (using `includes()`) have a time complexity of O(n), where n is the length of the array. This can lead to slower performance for large arrays. **Sets:** Pros: * Set lookups (using `has()`) have an average time complexity of O(1), making them faster than array lookups. * Sets automatically remove duplicates, which can be beneficial in some cases. Cons: * Sets are less familiar to developers, requiring more understanding and implementation effort. * Creating a set from an array involves iterating over the entire array, which can lead to slower performance for large arrays. **Other considerations** When choosing between using an array and a Set, consider the following factors: * **Duplicates**: If you need to store duplicate values, arrays might be a better choice. Otherwise, Sets are more suitable. * **Performance**: For small to medium-sized collections, the difference in performance is likely negligible. However, for large collections, Sets may offer better performance due to their O(1) lookup time complexity. * **Memory usage**: Arrays typically consume less memory than Sets, especially when storing duplicate values. **Alternative approaches** Other alternatives to consider: * **Objects**: If you need to store key-value pairs, objects might be a more suitable choice. Object lookups have an average time complexity of O(1), making them faster than array or Set lookups. * **Maps**: Similar to Sets, Maps are data structures that allow for fast lookup times (O(1) on average). They can be used instead of Sets when you need to store key-value pairs. In summary, while there is no significant performance difference between using arrays and Sets in this specific use case, Sets offer better performance for large collections due to their O(1) lookup time complexity. When choosing between the two, consider factors like duplicates, performance, memory usage, and alternative approaches.
Related benchmarks:
set vs array iteration
Includes (array) vs Has (Set)
Small n set vs array
set vs array find if exists
Comments
Confirm delete:
Do you really want to delete benchmark?