Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Set vs Array lookup (existing set)
(version: 1)
Comparing performance of:
Array Lookup vs Set Lookup
Created:
one year ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<!--your preparation HTML code goes here-->
Script Preparation code:
const arr = [1,2,3,4,5] const set = new Set([1,2,3,4,5])
Tests:
Array Lookup
arr.includes(5)
Set Lookup
set.has(5)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Array Lookup
Set Lookup
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one month ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/146.0.0.0 Safari/537.36
Browser/OS:
Chrome 146 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Array Lookup
38025520.0 Ops/sec
Set Lookup
77877960.0 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated one year ago):
The benchmark presented tests the performance of two different methods for checking the existence of an element in a collection: an array and a set. Specifically, it compares the use of the JavaScript `Array.prototype.includes()` method with the `Set.prototype.has()` method. ### Benchmark Breakdown **1. Benchmark Setup:** - **Array:** `const arr = [1,2,3,4,5]` creates a simple array containing five numeric elements. - **Set:** `const set = new Set([1,2,3,4,5])` constructs a Set with the same five elements. Sets in JavaScript are collections of values that are unique and unordered, allowing for quick lookups. **2. Individual Test Cases:** - **Array Lookup (`arr.includes(5)`):** - This test checks if the array `arr` contains the number `5`. The method `includes()` has a time complexity of O(n) because it may need to iterate through the entire array to find the specified value. - **Set Lookup (`set.has(5)`):** - This test checks if the set `set` contains the number `5`. The method `has()` has a time complexity of O(1) due to the underlying hash table that allows for rapid lookups. ### Performance Results From the benchmark results: - **Set Lookup:** `242,087,872` executions per second. - **Array Lookup:** `130,556,832` executions per second. ### Analysis of Results **Pros and Cons:** - **Array:** - **Pros:** - Simplicity: The array structure is straightforward and familiar to many developers. - Versatility: Works well for small datasets and when order matters. - **Cons:** - Slower lookups for larger datasets due to O(n) time complexity. - Performance suffers as the size of the data increases. - **Set:** - **Pros:** - Fast lookups: Constant time complexity (O(1)) for checking existence. - No duplicate values: Ensures that only unique elements are stored, which can optimize memory usage. - **Cons:** - Additional overhead: Creates a unique data structure that may not be as intuitive for all scenarios. - Limited functionality compared to arrays (e.g., elements in a set are unordered). ### Other Considerations - **Usage Context:** - If your application frequently requires checking for the existence of items in a collection, particularly with larger datasets, a `Set` is preferable. Conversely, if you need array methods (such as sorting or mapping), stick with arrays for small datasets. - **Alternatives:** - For functions similar to lookup operations, you could consider using an object for key-value pairs if the keys are strings. However, be cautious regarding key collisions. - For specialized searches, JavaScript’s `Map` can also be used, which allows for more sophisticated key-value storage and retrieval. ### Conclusion In the benchmark, using a `Set` for lookups demonstrates significantly better performance over using an array due to its optimized structure for this specific purpose. Software engineers should select the right data structure based on their specific requirements, taking into account the trade-offs in terms of speed and functionality.
Related benchmarks:
set.has vs. array.includes
set.has vs. array.includes (1m)
set.has vs. array.includesdd
set.has vs. array.includes first element
set.has vs. array.includes v22
set.has vs. array.includes (0)
set vs array lookup
array.includes vs. set.has on the fly
set.has (w/ creation) vs. array.includes
Comments
Confirm delete:
Do you really want to delete benchmark?