Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
set.has vs. array.includes 1e5
(version: 0)
Comparing performance of:
includes vs has
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var a = new Array(1e5); var lastIndex = a.length - 1; for(i=0, I = a.length; i < I; i++) { a[i] = i; } var b = new Set(a);
Tests:
includes
return a.includes(lastIndex)
has
return b.has(lastIndex)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
includes
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 what's being tested in the provided JSON benchmark. The benchmark is comparing two approaches for checking if an element exists in a data structure: `set.has()` and `array.includes()`. The test case uses two arrays of size 1e5 (100,000 elements) with values from 0 to 99,999. The array `a` is prepared by setting each element to its index value. **Approaches being compared:** 1. **`set.has()`**: This method is part of the JavaScript `Set` object, which automatically removes duplicates and provides fast membership testing. 2. **`array.includes()`**: This method is used for searching an element within a given array. It iterates through the array to find the specified value. **Pros and Cons:** * **`set.has()`**: + Pros: Fast (O(1) average time complexity), efficient, and accurate. + Cons: Requires a `Set` object, which may not be suitable for all use cases. Also, this method only works on JavaScript engines that support `Set`. * **`array.includes()`**: + Pros: Simple to implement, widely supported by most browsers and environments. + Cons: Iterates through the entire array (O(n) time complexity), which can lead to slower performance for large datasets. The test users special JS feature or syntax: None mentioned. **Other considerations:** * The benchmark uses a very large dataset (1e5 elements) to emphasize the performance difference between these two approaches. * The use of `Set` for this specific task may not be necessary, as the array already contains unique values. However, using `Set` can provide a more efficient solution if you need to perform membership testing on a set of elements. **Alternatives:** For smaller datasets, you might want to consider using other methods like: * Using a `Map` or an object for fast lookup. * Utilizing the `in` operator with arrays or sets. However, for large datasets like in this benchmark, `set.has()` will likely outperform `array.includes()` due to its O(1) time complexity.
Related benchmarks:
convert to set + set.has vs. array.includes
set.has vs. array.includes asdfasdfasdf
Small n set vs array
set vs array find if exists
Array includes vs Set.has
Comments
Confirm delete:
Do you really want to delete benchmark?