Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
small set vs array lookup 2
(version: 1)
Comparing performance of:
array vs set
Created:
one year ago
by:
Guest
Jump to the latest result
Script Preparation code:
const a = [1, 2]; const b = new Set(a); const items = [1, 2, 3];
Tests:
array
const pick = items[Math.floor(Math.random() * items.length)]; a.includes(pick);
set
const pick = items[Math.floor(Math.random() * items.length)]; b.has(pick);
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:
3 months ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64; rv:146.0) Gecko/20100101 Firefox/146.0
Browser/OS:
Firefox 146 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
array
32217154.0 Ops/sec
set
376280032.0 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated one year ago):
The benchmark provided tests the performance of two different approaches for checking the existence of an item in a collection: using an array (via the `includes` method) and using a `Set` (via the `has` method). Here's a detailed explanation of the test cases, their pros and cons, and some alternatives. ### Benchmark Overview 1. **Data Structure Preparation**: - An array `a` is initialized with the values `[1, 2]`. - A `Set` object `b` is created from the array `a`. A `Set` is a collection of unique values that supports fast lookups. - An array `items` is created containing the values `[1, 2, 3]`, which will be used for the existence check in both test cases. ### Test Cases #### 1. **Array Lookup** - **Test Name**: "array" - **Benchmark Definition**: The script randomly picks an element from `items` and checks if it exists in the original array `a` using the `includes` method. - **Performance**: This method involves scanning through the array to find the value, making it an O(n) operation in terms of time complexity. #### 2. **Set Lookup** - **Test Name**: "set" - **Benchmark Definition**: The script picks an item from `items` and checks if it is present in the `Set` using the `has` method. - **Performance**: This method benefits from the properties of sets, as checking for existence using `has` is generally O(1) (constant time complexity). ### Performance Results - The results show that the `Set` approach has a significantly higher execution rate: - **Set (has)**: 58,070,392 executions per second - **Array (includes)**: 32,367,820 executions per second - This indicates that using a `Set` for existence checks is much more efficient than using an array, especially as the size of the dataset increases. ### Pros and Cons #### Array Lookup Pros: - Simple to implement and straightforward for small datasets. - Well understood and widely usable in JavaScript. #### Array Lookup Cons: - Slower performance for larger datasets due to linear search time. - Inefficient if the dataset has many elements and frequent lookups are required. #### Set Lookup Pros: - Fast performance due to constant time complexity for membership checks. - Efficient when checking for the existence of a large number of items repeatedly. #### Set Lookup Cons: - Slightly more memory overhead compared to arrays because sets must store unique entries. - May not be as familiar to developers who primarily use arrays for similar tasks. ### Other Considerations - **Use Cases**: When to choose one over another largely depends on the use case. If you simply need to check existence in a small dataset or utilize order and duplicates, arrays can suffice. If performance is a key concern or the dataset is large, `Set` makes more sense. - **Alternatives**: Other alternatives could include using plain objects for dictionary-like lookups, which also provide average O(1) time complexity for existence checks, though they lack the automatic uniqueness and iterating properties of `Set`. In conclusion, the benchmark effectively demonstrates the performance differences between array and set lookups, highlighting the advantages of using `Set` for membership checks in scenarios where speed and efficiency are critical.
Related benchmarks:
test filtering with set
includes vs has
Set.has vs. Array.includes
set.has vs. array.includes asdfasdfasdf
set.has vs. array.includes 1e5
includes vs has 20220126
set.has vs. array.includes v2
Find the index of an array which does not exist in another one.
small set vs array lookup
Comments
Confirm delete:
Do you really want to delete benchmark?