Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
set.has vs. array.includes (large: 10000)
(version: 0)
Comparing performance of:
includes vs lookup
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var a = new Array(10000).fill(0).map((_, i) => i); var b = new Set(a)
Tests:
includes
return a.includes(9)
lookup
return b.has(9)
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.1:latest
, generated one year ago):
Let's dive into the details of this benchmark. **What is being tested?** This benchmark compares two different approaches to check if a value exists in a large dataset: 1. `array.includes()` method, which is used on an array `a` containing 10,000 elements. 2. `Set.has()` method, which is used on a set `b` created from the same array `a`. **What options are being compared?** The benchmark is comparing two different JavaScript functions to check if a value (in this case, the number `9`) exists in a large dataset: 1. The first function uses the `array.includes()` method on an array `a`. 2. The second function uses the `Set.has()` method on a set `b` created from the same array `a`. **Pros and Cons of each approach:** * **Array.includes()**: This method is simple to use, but it has a time complexity of O(n), where n is the length of the array. In this case, since we're dealing with a large array of 10,000 elements, this approach might be slower. + Pros: Easy to use and understand. + Cons: May be slow for very large datasets. * **Set.has()**: This method has an average time complexity of O(1), making it much faster than `array.includes()` for large datasets. Sets are particularly useful when you need to perform fast lookups or membership tests. + Pros: Fast and efficient for large datasets. + Cons: May require more setup (creating a set) and understanding of how sets work. **Other considerations:** * The benchmark script prepares an array `a` with 10,000 elements by mapping over the numbers from 0 to 9,999. This is done to create a large dataset that can be used for testing. * The Set `b` is created from this array using the `new Set(a)` syntax. This creates a set that contains all unique values from the original array. **Library usage:** None in this case. **Special JS features or syntax:** None mentioned in this benchmark. **Alternatives:** Other alternatives for checking membership in a large dataset include: 1. Using a `Map` instead of an `Array` or a `Set`. Maps can provide fast lookups with O(1) time complexity. 2. Implementing your own custom data structure, such as a hash table or a trie, which can be optimized for specific use cases. However, keep in mind that the use case presented here is specifically designed to test the performance of `array.includes()` and `Set.has()`, so these alternatives are not directly relevant to this benchmark.
Related benchmarks:
convert to set + set.has vs. array.includes
set.has vs. array.includes 2
set.has vs. array.includes (find 999,999 in 1,000,000)
Array.includes vs Set.has vas Map.has 2
Array.includes vs Set.has vas Map.has with large data set
Comments
Confirm delete:
Do you really want to delete benchmark?