Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
set.has vs. array.includes (10000 items)
(version: 0)
Comparing performance of:
includes vs lookup
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var a = new Array(10000).fill(0).map((x, i) => i) var b = new Set(a)
Tests:
includes
return a.includes(5000)
lookup
return b.has(5000)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
includes
lookup
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 benchmark and explain what's being tested, compared, and considered. **Benchmark Overview** The benchmark measures the performance difference between using `Array.includes()` and `Set.has()` for large arrays. The test creates an array of 10,000 integers (from 0 to 9,999) and a Set from this array. Then, it runs two test cases: 1. `includes`: searches for a specific element (5,000) within the original array using `Array.includes()`. 2. `lookup`: searches for the same element (5,000) in the Set created earlier using `Set.has()`. **Comparison Options** The benchmark compares two approaches: 1. **`Array.includes()`**: This method iterates through the array to find a specific element by checking each element's value against the target value. 2. **`Set.has()`**: This method checks if a specific value exists in the Set, which is implemented as an unordered collection of unique values. **Pros and Cons** * **`Array.includes()`**: * Pros: Easy to implement, widely supported by most browsers. * Cons: Can be slow for large arrays due to linear search. * **`Set.has()`**: * Pros: Efficient for checking membership in a Set, as it only needs to traverse the set's internal data structure. * Cons: Not all browsers support Sets, and implementing them can add complexity. **Other Considerations** * The benchmark does not consider other factors that might affect performance, such as array initializers or data structures. (Though in theory they can have a impact) * Other optimization methods like caching lookups to avoid repeated checks on the same value might also be beneficial for large arrays **Library and Special JS Feature** The test uses two standard JavaScript library functions: 1. **`Array.includes()`**: Part of the JavaScript Array prototype. 2. **`Set.has()`**: Part of the JavaScript Set prototype. No special JavaScript features are used in this benchmark, as they might not be supported by older browsers or might introduce unnecessary complexity for a simple performance comparison. **Alternatives** Other alternatives to `Array.includes()` and `Set.has()` include: * For large arrays: * **`Binary Search`**: An efficient algorithm for finding an element in an ordered array. However, it's more complex to implement. * **`Hash Tables`**: Data structures that store key-value pairs with fast lookup times. However, they might not be suitable for all use cases and have additional memory requirements. * For Sets: * **`Map`**: A data structure similar to a Set but with key-value associations. It provides fast lookups, insertions, and deletions. * **`WeakMaps`**: A type of Map that can be used when working with weak references. Overall, the benchmark helps determine which method is more efficient for searching in large arrays or Sets. However, depending on specific requirements, other optimization techniques and data structures might provide better performance.
Related benchmarks:
set.has vs. array.includes 2
set.has vs. array.includes on 25 000 items
set.has vs. array.includes (find 999,999 in 1,000,000)
set.has vs. array.includes for 1000 elements
Comments
Confirm delete:
Do you really want to delete benchmark?