Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array.every => Array.includes vs Array.every => Set.has with new Set() creation
(version: 5)
Comparing performance of:
Array.every => Array.includes vs Array.every => Set.has with new Set() creation
Created:
one year ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var object = { items: Array.from({ length: 100 }, (_, i) => ({ id: i, name: `Item-${i}`, })) }; // Randomly select ~75% of items var selectedItems = object.items .filter(() => Math.random() > 0.25) .map(item => item.id);
Tests:
Array.every => Array.includes
return object.items.every(({ id }) => selectedItems.includes(id));
Array.every => Set.has with new Set() creation
return ((selected) => object.items.every(({ id }) => selected.has(id)))( new Set(selectedItems) );
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Array.every => Array.includes
Array.every => Set.has with new Set() creation
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/133.0.0.0 Safari/537.36
Browser/OS:
Chrome 133 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Array.every => Array.includes
44636192.0 Ops/sec
Array.every => Set.has with new Set() creation
840470.5 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated one year ago):
### Benchmark Overview The benchmark compares two different approaches to check if every item in an array is included in a set of selected IDs. The first approach utilizes the `Array.every` method combined with `Array.includes`, while the second approach employs `Array.every` along with `Set.has` for a set created from `selectedItems`. This comparison highlights the efficiency differences between these two techniques in checking for the presence of elements. ### Test Cases Explained 1. **Test Case 1: `Array.every => Array.includes`** - **Benchmark Definition**: `return object.items.every(({ id }) => selectedItems.includes(id));` - **Description**: This method iterates through each item in the `object.items` array and checks if its `id` is included in the `selectedItems` array using `Array.includes`. - **Pros**: - Simple and straightforward to understand. - No additional data structures needed. - **Cons**: - `Array.includes` performs a linear search, which results in O(n*m) complexity, where n is the size of `object.items` and m is the size of `selectedItems`. This can lead to performance bottlenecks if both arrays are large. 2. **Test Case 2: `Array.every => Set.has with new Set() creation`** - **Benchmark Definition**: `return ((selected) => object.items.every(({ id }) => selected.has(id)))(new Set(selectedItems));` - **Description**: In this approach, a new `Set` is created from the `selectedItems` array. The `Array.every` method is used again, but this time it checks if the `Set` contains each `id` via `Set.has`. - **Pros**: - `Set.has` provides average constant time complexity O(1) for lookup, leading to an overall time complexity of O(n) for this test case. This is significantly better than the previous approach, especially as the sizes of the arrays grow. - Reduces the number of lookups since a `Set` is optimized for such operations. - **Cons**: - Additional memory overhead for creating a `Set` from `selectedItems`. - There’s a performance penalty for the initial creation of the `Set`, but this is generally offset by the faster lookups. ### Benchmark Results Analysis From the benchmark results, we see the following performance metrics: - **`Array.every => Array.includes`**: Executes approximately **44,636,192** times per second. - **`Array.every => Set.has with new Set() creation`**: Executes approximately **840,470** times per second. ### Conclusion and Alternatives The results clearly indicate that the `Set.has` approach is significantly faster than using `Array.includes`, highlighting the benefits of using data structures optimized for performance in specific use cases. **Other Alternatives**: 1. **Using a traditional loop**: One might use a for-loop along with `Array.includes` or `Set.has` to avoid the overhead of creating a new `Array` or `Set` iteratively. 2. **Using `filter` + `length`**: This could be another way to check for inclusion, but it would also face the same performance limitations as the `Array.includes` method. 3. **Using libraries**: Libraries like Lodash offer utility functions such as `_.intersection` to find common elements, which may provide better performance in certain scenarios. However, the benefits would heavily depend on how these are implemented under-the-hood. Overall, understanding the distinctions between these approaches allows software engineers to choose the most efficient method based on the size of the data set and the specific requirements of their application.
Related benchmarks:
Contains, indexOf
perf check
Unique values (large list)
reduce vs filter+map
uniq indexOf vs lodash uniq
Array.find vs Array.filter on large dataset
Populating a Set
Array.every => Array.includes vs Array.every => Set.has with set creation
Array.every => Array.includes vs Array.every => Set.has
Comments
Confirm delete:
Do you really want to delete benchmark?