Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
set.has vs. array.includes xd
(version: 0)
Comparing performance of:
array -> includes vs set -> has
Created:
5 years ago
by:
Guest
Jump to the latest result
Tests:
array -> includes
var a = ["TEST_1", "TEST_2", "TEST_3", "TEST_4", "TEST_5", "TEST_6", "TEST_7", "TEST_8", "TEST_9", "TEST_10"]; return a.includes("TEST_9") && a.includes("TEST_8") && a.includes("TEST_7") && a.includes("TEST_6") && a.includes("TEST_5") && a.includes("TEST_4") && a.includes("TEST_3");
set -> has
var a = ["TEST_1", "TEST_2", "TEST_3", "TEST_4", "TEST_5", "TEST_6", "TEST_7", "TEST_8", "TEST_9", "TEST_10"]; var b = new Set(a) return b.has("TEST_9") && b.has("TEST_8") && b.has("TEST_7") && b.has("TEST_6") && b.has("TEST_5") && b.has("TEST_4") && b.has("TEST_3");
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 break down the benchmark and explain what's being tested. **Benchmark Definition** The benchmark is testing two different approaches to check if an element exists in a data structure: 1. **Array includes**: The first test case uses the `includes()` method on an array to check if a specific string (`"TEST_9"` in this case) is present in the array. 2. **Set has**: The second test case uses the `has()` method on a Set object created from the same array as above. **Options Compared** The two options being compared are: 1. `includes()` method on an array 2. `has()` method on a Set object **Pros and Cons of Each Approach** **Array includes:** Pros: * Simple and straightforward to use * Wide support across browsers and platforms * Fast lookup times for small arrays Cons: * Can be slow for large arrays ( O(n) complexity) * May not perform well with duplicate elements in the array * May not be suitable for cases where order of elements matters **Set has:** Pros: * Fast lookup times ( O(1) complexity) even for large sets * Efficient use of memory, as duplicates are automatically removed * Suitable for cases where uniqueness is important Cons: * Requires creating a Set object from the original data structure * May not be suitable for cases where order of elements matters * Browser support may vary, although modern browsers have excellent Set implementations. **Library/Language Features Used** In this benchmark, no libraries or special JavaScript features are used beyond what's built into the standard language. The `Set` object is a native feature in ECMAScript 2015 (ES6) and later versions. **Other Considerations** * **Array vs. Set**: When to use each? Use an array when you need to preserve the original order of elements, or when you need to perform operations that rely on indexing (e.g., `array.indexOf()`). Use a Set when you need fast lookup times for unique elements. * **Browser Support**: While modern browsers have excellent implementations of these methods, some older browsers may not support them at all. Ensure your target audience is compatible with the chosen method. **Alternatives** If you're looking for alternative approaches, consider: 1. **Using a different data structure**, such as an object or a trie, depending on the specific requirements. 2. **Implementing a custom lookup mechanism**, tailored to your specific use case and performance requirements. 3. **Using a third-party library** that provides optimized implementations of these methods. Keep in mind that the choice of approach depends on the specific requirements of your project, including performance, memory constraints, and compatibility with different browsers and platforms.
Related benchmarks:
convert to set + set.has vs. array.includes
new Set([x]).has vs [x].includes
array.includes vs. set.has on the fly
Array includes vs Set.has
set.has (w/ creation) vs. array.includes
Comments
Confirm delete:
Do you really want to delete benchmark?