Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
includes vs has
(version: 0)
Comparing performance of:
includes vs has
Created:
5 years ago
by:
Guest
Jump to the latest result
Tests:
includes
const array = [1, 2, 3]; const hasValue = value => array.includes(value); hasValue(1); hasValue(2); hasValue(3);
has
const set = new Set([1, 2, 3]); const hasValue = value => set.has(value); hasValue(1); hasValue(2); hasValue(3);
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:
10 months ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36
Browser/OS:
Chrome 129 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
includes
40222540.0 Ops/sec
has
14591688.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Measuring JavaScript performance is a fascinating topic! Let's dive into the provided benchmark. **Benchmark Overview** The benchmark compares two approaches for checking if a value exists in an array: `includes()` and `has()`. The test cases are designed to measure the execution time of these methods on both arrays and sets. **Includes vs. Has** ### Includes `includes()` is a method that checks if an element with the specified value exists in the array. It returns `true` if the element is found, and `false` otherwise. Pros: * Widely supported by most JavaScript engines. * Can be used on arrays of any type (primitive values, objects, etc.). Cons: * May iterate over the entire array for each check, resulting in O(n) time complexity. * Can be slower than other approaches for large datasets. ### Has `has()` is a method that checks if an element with the specified value exists in the set. It returns `true` if the element is found, and `false` otherwise. Pros: * Typically faster than `includes()`, especially for sets, since it uses a hash table lookup. * Designed specifically for sets, which can provide better performance than arrays. Cons: * Less widely supported by JavaScript engines compared to `includes()` (although becoming more common). * May not work on all types of sets (e.g., arrays). **Library Usage** In the provided benchmark, the `Set` object is used in one of the test cases (`has`). The `Set` library is a built-in JavaScript API that provides efficient data structures for storing unique values. Its primary purpose is to: * Provide fast membership testing (`has()`) and insertion/removal operations. * Offer an alternative to arrays or objects when dealing with large amounts of unique data. **Special JS Feature** There is no special JavaScript feature or syntax explicitly used in this benchmark, but note that the use of `const` and arrow functions (`=>`) are modern JavaScript features introduced in ECMAScript 2015 (ES6). **Other Alternatives** Some alternative approaches for checking if a value exists in an array or set include: * Using `indexOf()` on arrays, which returns -1 if not found. * Implementing a custom search algorithm using loops and bitwise operations. * Utilizing other data structures like trees or graphs to store the data. However, these alternatives are often less efficient, more complex to implement, or may not be supported by all JavaScript engines.
Related benchmarks:
new Set([x]).has vs [x].includes
two condition if vs includes compare
array.includes vs. set.has on the fly
String equals vs String.includes
equals vs includes (one value)
Comments
Confirm delete:
Do you really want to delete benchmark?