Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Set Has vs Array Includes Smaller
(version: 0)
Comparing performance of Set.has() vs Array.includes()
Comparing performance of:
Array Includes vs Set Has
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var array = Array.from(Array(5), (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 break down the provided JSON benchmark definition and test cases. **Benchmark Definition** The benchmark is designed to compare the performance of two JavaScript methods: `Array.includes()` and `Set.has()`. The purpose of this benchmark is to determine which method is more efficient for checking if an element exists in a given array or set. **Script Preparation Code** The script preparation code generates a random array of numbers from 0 to 4 (inclusive) and sorts it. This array will be used as the input data for both `Array.includes()` and `Set.has()`. The sorting is done using a subtle trick: `(() => 0.5 - Math.random())` creates an unpredictable order, which helps to avoid optimizations that might favor one method over the other. **Html Preparation Code** The html preparation code is empty, indicating that no HTML elements are involved in this benchmark. **Individual Test Cases** There are two test cases: 1. **Array Includes**: This test case uses a simple loop to iterate over the array and call `array.includes(i)` for each element. 2. **Set Has**: This test case uses another simple loop to iterate over the set and call `set.has(i)` for each element. **Library Used** In this benchmark, the `Array.from()` method is used to create an array from an iterable (in this case, a callback function that generates numbers). The `Set` class is also used to create a new set from the array. **Special JS Feature/Syntax** None are mentioned in this benchmark. However, it's worth noting that the use of `Array.from()` and `Set` is a modern JavaScript feature that was introduced in ECMAScript 2015 (ES6). **Pros and Cons of Different Approaches** Here's a brief analysis of the two approaches: * **Array Includes**: This method is generally faster for small to medium-sized arrays because it uses a linear search algorithm. However, as the array size increases, the time complexity becomes O(n), which can be slower than other methods. * **Set Has**: For large datasets, `Set.has()` tends to perform better due to its O(1) average time complexity, making it more efficient for lookup operations. **Other Alternatives** If you wanted to explore alternative approaches, here are a few: * **Regular expressions**: You could use regular expressions to achieve the same result as `Array.includes()`. However, this would likely be slower than both `Array.includes()` and `Set.has()`. * **Sparse arrays**: If you're working with sparse arrays (i.e., arrays where most elements are null or undefined), you might be able to optimize the lookup by using a sparse array data structure. However, this would require significant changes to the implementation. * **Native libraries**: Depending on your target platform and browser support, you could use native libraries like `nativeArray.includes()` or `Set.prototype.has()` (if available) for improved performance. Keep in mind that these alternatives might have their own trade-offs in terms of compatibility, maintainability, and optimization opportunities.
Related benchmarks:
Set Has vs Array Includes
set.has vs. array.includes Performance 2
set.has vs. array.includes Perf Random
set.has vs. array.includes Perf Random 1
Comments
Confirm delete:
Do you really want to delete benchmark?