Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Set Has vs Array Includes
(version: 0)
Comparing performance of Set.has() vs Array.includes()
Comparing performance of:
Array Includes vs Set Has
Created:
5 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var array = Array.from(Array(50), (x, index) => index + 1).sort(() => 0.5 - Math.random()); var set = new Set(array);
Tests:
Array Includes
for (let i = 0; i < array.length; i++) { array.includes(i); }
Set Has
for (let i = 0; i < array.length; i++) { set.has(i); }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Array Includes
Set Has
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the explanation of the provided benchmark. **What is being tested?** The provided benchmark compares the performance of two approaches: 1. `Array.includes()` method 2. `Set.has()` method Both methods are used to check if an element exists in a collection (an array or a set, respectively). **Options compared:** * **Array.includes()**: This method takes an element as an argument and returns a boolean value indicating whether the element is present in the array. * **Set.has()**: This method takes an element as an argument and returns a boolean value indicating whether the element is present in the set. **Pros and Cons of each approach:** * **Array.includes():** + Pros: - Simple and intuitive syntax - Efficient for small to medium-sized arrays + Cons: - May involve linear search, leading to O(n) time complexity - Can be slower for large arrays due to the search operation * **Set.has():** + Pros: - Fast lookup time (O(1) average case) - Efficient for large sets or collections + Cons: - Requires a set data structure, which may incur additional memory and initialization overhead - May not be suitable for all types of data (e.g., non-hashable elements) **Library used:** The `Array.from()` method is used to create an array from an iterable. In this benchmark, it's used to generate an array with 50 random elements. No library is explicitly mentioned in the test cases or benchmark definition. **Special JS feature/syntax:** None of the provided code snippets contain any special JavaScript features or syntax that would require additional explanation. **Other alternatives:** If you need to check if an element exists in a collection, other alternatives could be: * `Array.indexOf()`: Similar to `includes()`, but returns the index of the first occurrence (or -1 if not found). * `Array.prototype.some()` or `Array.prototype.every()`: These methods can be used with callback functions to test for presence or absence of elements in an array. * Using a different data structure, such as a Map or a Set-like object (e.g., `Map.of()`), might also provide alternative solutions. Keep in mind that the choice of approach depends on the specific requirements and constraints of your use case.
Related benchmarks:
set.has vs. array.includes Performance 2
set.has vs. array.includes Perf Random
set.has vs. array.includes Perf Random 1
Set Has vs Array Includes Smaller
Comments
Confirm delete:
Do you really want to delete benchmark?