Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
find: .includes vs hashtable .has
(version: 0)
Comparing performance of:
.includes vs hashtable .has
Created:
one year ago
by:
Guest
Jump to the latest result
Script Preparation code:
var LIST = Array.from({ length: 10000 }, (_, idx) => idx); var VALUE = 7000;
Tests:
.includes
const hasItem = LIST.includes(VALUE);
hashtable .has
const listToMap = LIST.reduce((map, item) => { map.set(item, item); return map; }, new Map) const hasItem = listToMap.has(VALUE);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
.includes
hashtable .has
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36
Browser/OS:
Chrome 127 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
.includes
881808.9 Ops/sec
hashtable .has
1742.5 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and its options. **What is tested:** The test measures the performance difference between two approaches: 1. `LIST.includes(VALUE)`: This method checks if an element exists in an array using the `includes` method. It iterates through the array until it finds the specified value or reaches the end of the array. 2. `hashtable .has(listToMap.has(VALUE))`: This method uses a hash table (in this case, a JavaScript Map) to store the elements of the `LIST`. The `has` method is then used to check if the specified value exists in the map. **Options compared:** The two options are: 1. **Array-based .includes**: This approach uses the built-in `includes` method on an array. 2. **Map-based .has**: This approach uses a JavaScript Map to store the elements of the list and then checks for existence using the `has` method. **Pros and Cons:** **Array-based .includes**: Pros: * Simple implementation * No extra memory allocation required * Well-documented Cons: * May be slower due to iteration through the array * Not suitable for large datasets or hash-based lookups **Map-based .has**: Pros: * Efficient for large datasets, as Map lookups are O(1) on average * Suitable for hash-based lookups and fast access times Cons: * More complex implementation compared to Array-based .includes * Requires extra memory allocation (the Map object itself) Other considerations: * **Memory usage**: The Map approach requires additional memory for storing the elements, while the Array-based .includes method only stores the value being searched. * **Cache performance**: The Map approach can be beneficial if caching is involved, as Maps have better cache performance due to their hash-based structure. **Library and purpose:** The `Map` object in JavaScript is a data structure that stores key-value pairs. It provides fast lookups, insertions, and deletions using the `has`, `set`, and `delete` methods, respectively. **Special JS feature or syntax:** There are no specific special features or syntaxes mentioned in this benchmark. **Benchmark preparation code explanation:** The Script Preparation Code creates an array of 10,000 elements with values from 0 to 9,999 using the `Array.from` method and a callback function that returns the index. The VALUE variable is set to 7,000. The Html Preparation Code is empty in this case. **Alternative approaches:** Other alternatives for implementing the `.includes` method could include: * Using `Array.indexOf()` or `Array.prototype.lastIndexOf()` * Implementing a custom binary search algorithm * Using a library like Lodash's `include` function However, the Array-based .includes approach is the most straightforward and widely supported implementation.
Related benchmarks:
find vs includes
IndexOf vs Includes vs find
set.has vs. array.includes (find 999,999 in 1,000,000)
find vs includes vs indexof
#2 Array Includes vs. Find
Comments
Confirm delete:
Do you really want to delete benchmark?