Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
set.has vs. array.includes2222adgag
(version: 0)
Comparing performance of:
includes vs lookup
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var a = new Array(1000000).fill(0).map((_, i) => i) var b = new Set(a)
Tests:
includes
return a.includes(91222)
lookup
return b.has(91222)
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 definition and test cases. **Benchmark Definition** The benchmark measures the performance difference between using `Array.includes()` and `Set.has()` on large arrays. The array contains 1,000,000 elements filled with zeros and mapped to their indices. A Set is created from this array. Then, two separate benchmark definitions are defined: 1. `return a.includes(91222)`: This test case checks the performance of using `Array.includes()` on a specific element (91222) in the large array. 2. `return b.has(91222)`: This test case checks the performance of using `Set.has()` on the same specific element. **Options Compared** The two options being compared are: * **Array.includes()**: A method that returns a boolean value indicating whether an element with the specified value exists in the array. * **Set.has()**: A method that returns a boolean value indicating whether an element with the specified value exists in the Set. **Pros and Cons of Each Approach** Here's a brief analysis of each approach: * **Array.includes():** + Pros: - Widely supported by most JavaScript engines. - Can be used on any array, not just indexed arrays. + Cons: - May involve linear search in the worst case, leading to slower performance for large arrays. - Does not provide information about the element's presence beyond a simple boolean value. * **Set.has():** + Pros: - Generally faster than `Array.includes()` for large datasets due to its hash-based lookup. - Provides more information about the element's presence, including its index (if it exists). + Cons: - May not be supported by all JavaScript engines or older versions of Chrome. **Library and Its Purpose** The `Set` data structure is a built-in part of modern JavaScript. It provides an efficient way to store unique values and perform lookups using the `has()` method. **Special JS Feature or Syntax (None)** There are no special features or syntax used in this benchmark that would require additional explanation. **Other Alternatives** If you were to use a different approach, some alternatives could be: * Using `Array.indexOf()` instead of `Array.includes()`, which returns the index of the element if found, or -1 otherwise. * Implementing a custom lookup algorithm using bit manipulation or other techniques for better performance on specific datasets. However, it's essential to note that these alternative approaches may not provide the same level of performance as the original benchmark and might require additional implementation effort.
Related benchmarks:
convert to set + set.has vs. array.includes
set.has vs. array.includes 2
has vs includes
Array.includes vs Set.has vas Map.has 2
Comments
Confirm delete:
Do you really want to delete benchmark?