Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
array.includes vs Set.has vs convert to set
(version: 1)
Comparing performance of:
includes vs has vs convert to set before includes
Created:
5 years ago
by:
Registered User
Jump to the latest result
HTML Preparation code:
<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js'></script>
Script Preparation code:
var arr = _.times(8000, n => `elem_${n}`); var se = new Set(arr)
Tests:
includes
arr.includes('elem_7999')
has
se.has('elem_7999')
convert to set before includes
var converted = new Set(arr); converted.has('elem_7999');
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
includes
has
convert to set before includes
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
7 months ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/141.0.0.0 Safari/537.36
Browser/OS:
Chrome 141 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
includes
71821.2 Ops/sec
has
228181120.0 Ops/sec
convert to set before includes
4700.4 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**Overview** The provided benchmark measures the performance of three different approaches to check if an element exists in an array or set: `arr.includes('elem_7999')`, `se.has('elem_7999')`, and converting the array to a set (`converted = new Set(arr)`) before checking. **Options Compared** 1. **`arr.includes('elem_7999')`**: This approach checks if an element with the specified value exists in the original array. 2. **`se.has('elem_7999')`**: This approach uses the `Set` data structure to check if an element exists. 3. **`converted = new Set(arr); converted.has('elem_7999')`**: This approach converts the array to a set, which allows for faster lookups. **Pros and Cons** 1. **`arr.includes('elem_7999')`**: * Pros: Simple, widely supported, no additional data structure required. * Cons: May be slower for large arrays due to the search operation. 2. **`se.has('elem_7999')`**: * Pros: Faster lookup times compared to `arr.includes()`, as sets use hash tables internally. * Cons: Requires a `Set` object, which may not be necessary in all cases. 3. **`converted = new Set(arr); converted.has('elem_7999')`**: * Pros: Can provide the best performance for large arrays, as set operations are typically faster than array searches. * Cons: Requires additional memory allocation and creation of a new data structure. **Library and Purpose** The benchmark uses Lodash's `_.times()` function to generate an array of 8000 elements. The `Set` data structure is used to provide fast lookups. **Special JS Feature/Syntax** None mentioned in the provided benchmark definition, but it's worth noting that using `Set.has()` can be a shorthand for creating a new set and then checking its existence. **Other Alternatives** 1. **Using `Array.prototype.indexOf()`**: This approach would search for the element by index, which may not be as efficient as using `includes()`. 2. **Using `Binary Search`**: For sorted arrays, binary search can provide significant performance improvements over linear searches. 3. **Using a different data structure**, such as a `Map`, could potentially offer better lookup times than sets. In summary, the benchmark provides valuable insights into the performance differences between these three approaches for checking if an element exists in an array or set.
Related benchmarks:
Spread Operator vs Lodash with not so many items
Comparing lodash's times with Array.from
Spread Operator vs Lodash [2]
Array From vs lodash clone
Comments
Confirm delete:
Do you really want to delete benchmark?