Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
has vs includes
(version: 0)
Comparing performance of:
includes vs has
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var arr = Array.from(100).fill().map((_, index) => index); var set = new Set(arr);
Tests:
includes
arr.includes(5);
has
set.has(5);
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:
Run details:
(Test run date:
6 months ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36
Browser/OS:
Chrome 130 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
includes
112901144.0 Ops/sec
has
144508832.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the world of JavaScript microbenchmarks on MeasureThat.net! The provided benchmark tests two different methods for checking if an element exists in a sequence or set: `includes()` and `has()`. These methods are used to determine whether a specific value is present in an array, set, or other sequences. **Options Compared** In this benchmark, the following options are compared: 1. **`arr.includes(5)`**: This method checks if the value `5` exists in the `arr` array using the `includes()` function. 2. **`set.has(5)`**: This method checks if the value `5` exists in the `set` set using the `has()` function. **Pros and Cons** * **`arr.includes(5)`**: * Pros: Simple, widely supported, and relatively fast for small arrays. * Cons: May be slower for large arrays due to its linear search algorithm. Additionally, it may not work correctly with non-numeric values or NaN (Not a Number) values. * **`set.has(5)`**: * Pros: Fast for sets and other sequences with fast membership testing, as it uses a hash-based data structure internally. * Cons: May require additional setup and memory to store the set, especially if dealing with large datasets. It also might be slower than `arr.includes()` for very small arrays. **Library/Function Purpose** The `Array.prototype.includes()` function and the `Set.prototype.has()` method are built-in JavaScript functions that are used to check for the presence of a value in an array or set, respectively. * **`includes()` Function**: This function returns `true` if an element with the specified value exists in the array; otherwise, it returns `false`. * **`has()` Method**: This method returns a boolean indicating whether the element with the specified key exists in the set. **Special JS Features/Syntax** In this benchmark, there is no use of special JavaScript features or syntax such as arrow functions, async/await, or modern JavaScript APIs like Promise.all() or fetch(). **Other Alternatives** If you're looking for alternative methods to check for element existence in an array or set, consider the following: * **`arr.indexOf(5)`**: This method returns the index of the first occurrence of `5` in the array. If `5` is not found, it returns `-1`. * **`set.size === 0 || set.has(5)```:** This approach uses the size property of the set to check if it's empty and then checks for the presence of the value using the `has()` method. * **Other libraries or frameworks**: Depending on your specific use case, you might consider using libraries like Lodash or Underscore.js, which provide additional utility functions, including membership testing methods. In conclusion, the provided benchmark on MeasureThat.net compares two common methods for checking element existence in an array: `includes()` and `has()`. The choice between these methods depends on your specific use case, performance requirements, and the size of your dataset.
Related benchmarks:
has vs includess
JS includes vs set
array.includes vs array.indexOf
Array.includes vs Set.has vas Map.has 2
Comments
Confirm delete:
Do you really want to delete benchmark?