Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
set vs array find if exists 2
(version: 1)
Comparing performance of:
array vs set
Created:
one year ago
by:
Guest
Jump to the latest result
Script Preparation code:
var a = []; for(i=0;i<10000;i++) a.push(i); var b = new Set(a)
Tests:
array
a.indexOf(342);
set
b.has(342);
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 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36
Browser/OS:
Chrome 108 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
array
591599424.0 Ops/sec
set
576916992.0 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated one year ago):
In the benchmark defined by MeasureThat.net titled "set vs array find if exists 2," two different data structures in JavaScript—an array and a `Set` object—are compared to determine their performance when checking for the existence of a specific value (in this case, the number 342). Below is a detailed breakdown of the benchmarking process, the tested options, their pros and cons, and general considerations. ### Tested Options 1. **Array (`indexOf`)**: - **Benchmark Definition**: `a.indexOf(342);` - **Test Name**: "array" 2. **Set (`has`)**: - **Benchmark Definition**: `b.has(342);` - **Test Name**: "set" ### Library and Syntax Features In this benchmark, no external JavaScript libraries are used; instead, it relies solely on built-in JavaScript features, specifically the `Array` and `Set` data structures. - **Array**: - The `indexOf` method is used, which searches for a specified element and returns its first index in the array. If the value is not found, it returns -1. This method has a time complexity of O(n) because it must iterate through the array to find the specified value. - **Set**: - The `has` method of the `Set` object checks for the existence of a value. The `Set` structure maintains unique values, offering faster lookups. The time complexity for this operation is generally O(1), given that `Set` is optimized for fast existence checks. ### Pros and Cons #### Array (`indexOf`) **Pros**: - Widely used and well-known by developers; easy to understand for basic use cases. - Suitable for small datasets where performance is not a significant concern. **Cons**: - Inefficient for large datasets due to linear time complexity (O(n)). - If the dataset grows or searching is frequent, performance will degrade. #### Set (`has`) **Pros**: - Highly efficient existence checks with average O(1) time complexity, making it excellent for large datasets. - Automatically manages unique values, preventing duplicates. **Cons**: - Slightly higher memory overhead compared to arrays, as it has to maintain additional internal structure for uniqueness. - Less intuitive for those who are only familiar with arrays, particularly for tasks that involve ordered or indexed collections. ### General Considerations Choosing between arrays and sets in the context of checking for the existence of a value largely depends on the specific requirements of the application. If the data being processed is relatively small and performance is not the primary criterion, arrays and their associated methods (like `indexOf`) can suffice. On the other hand, if performance and scalability are priorities—especially when dealing with frequent lookups or large datasets—using a `Set` is a better approach. ### Other Alternatives While this benchmark focuses on `Array` and `Set`, there are other alternatives to consider: - **Maps**: For checks based on key-value pairs, `Map` provides fast lookups similar to `Set` but for structured data. - **Objects**: Using objects to leverage keys as a simple existence check, although typically less efficient than a `Set`. - **Other data structures**: Depending on the programming environment, there may be other data structures like hash tables or binary search trees that could serve for existence checks. In conclusion, the benchmark illustrates a clear performance difference between using an array and a set to verify the existence of an element, favoring the `Set` approach for larger datasets or frequent searches due to its optimized time complexity.
Related benchmarks:
set vs array includes
set.has vs. array.includes large 2
array.includes vs set.has 2022
set.has vs. array.includes 1000
set.has vs. array.includes 1M
Object VS new Set ()
set.has vs. array.includes (large array)
set vs array includestratsatsrats
new set vs array includes
Comments
Confirm delete:
Do you really want to delete benchmark?