Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Set vs array performace compare
(version: 1)
Comparing performance of:
Array vs Set
Created:
one year ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<!--your preparation HTML code goes here-->
Script Preparation code:
var a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; var b = new Set(a)
Tests:
Array
a.includes(5)
Set
b.has(5)
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:
7 days ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/147.0.0.0 Safari/537.36
Browser/OS:
Chrome 147 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Array
70239504.0 Ops/sec
Set
125026080.0 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated one year ago):
The benchmark aims to compare the performance of two data structures in JavaScript: Arrays and Sets. Specifically, it tests how quickly each structure can determine the presence of a specific element, in this case, the number `5`. ### Options Compared 1. **Array (`a.includes(5)`)**: - This test checks if the array contains the element `5`. The method `.includes()` is used, which sequentially scans the array until it finds the specified element or reaches the end. 2. **Set (`b.has(5)`)**: - This test checks whether a Set contains the element `5`. The method `.has()` checks for the existence of the element and typically does so in constant time (O(1)), as Sets are implemented using a hash table. ### Performance Results The benchmark results indicate that the Set data structure significantly outperformed the Array in terms of execution speed: - **Set**: 154,915,264 executions per second - **Array**: 91,397,872 executions per second This shows that checking for membership in a Set is substantially faster than checking for membership in an Array. ### Pros and Cons #### Array - **Pros**: - Simple and intuitive structure, and easy to use for small lists of items. - Supports ordering of elements and allows for duplicate values. - **Cons**: - Membership checks are inefficient for large arrays (O(n) time complexity with `.includes()`). - Performance can degrade as the array size increases because it must scan through each element to find matches. #### Set - **Pros**: - Offers O(1) average time complexity for membership checks using `.has()`, making it ideal for scenarios where fast lookups are critical. - Automatically handles uniqueness, so duplicate entries are not allowed—a feature that can aid in simplifying logic errors. - **Cons**: - May be less intuitive for engineers unfamiliar with its properties, especially if they are more accustomed to working with arrays. - Uses more memory than an array due to the underlying hash table structure. ### Other Considerations - **Use Case**: Choosing between Array and Set should depend on the use case. For scenarios where you need to frequently check for the existence of elements, Sets are usually preferable. If you require ordered collections or need to manage duplicates, Arrays might be more appropriate. - **Memory Usage**: Sets use more memory than arrays because they store additional information for their internal hashing, which might be a consideration in memory-constrained environments. ### Alternatives - **Objects**: For scenarios that require key-value pairs, JavaScript objects can be used. They offer similar advantages for checking existence, though with unique key constraints. - **Maps**: For more complex key-value associations, Maps can provide better performance characteristics and offer keys of any type as opposed to just strings. In summary, this benchmark effectively demonstrates the performance difference between looking up items in an Array versus a Set, with Sets emerging as the more efficient choice for membership checks. Understanding when to use each data structure can greatly enhance performance in JavaScript applications.
Related benchmarks:
set.has vs. array.includes
set vs array
set.has vs. array.includes - including extra code for more fair comparison as typically you will have an array and not a set
set.has vs. array.includes perf
Includes (array) vs Has (Set)
Small n set vs array
set.has vs. array.includes first element
set.has vs. array.includes v22
set vs array find if exists
set.has vs. array.includes (0)
Comments
Confirm delete:
Do you really want to delete benchmark?