Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
set.has vs. array.includes vs array.find vs map.has (20 elements)
(version: 0)
Comparing performance of:
Array.includes vs Set.has vs Array.find vs Map.has
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]; var b = new Set(a) var c = new Map(); a.forEach(x => c.set(x, x)) // just for test.
Tests:
Array.includes
return a.includes(19)
Set.has
return b.has(19)
Array.find
return a.find(x => x === 19)
Map.has
return c.has(9)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
Array.includes
Set.has
Array.find
Map.has
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.5993.821 YaBrowser/23.11.2.821 Yowser/2.5 Safari/537.36
Browser/OS:
Yandex Browser 23 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Array.includes
27949182.0 Ops/sec
Set.has
27225324.0 Ops/sec
Array.find
22223546.0 Ops/sec
Map.has
23196666.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided JSON and explain what's being tested. **Benchmark Definition** The benchmark is designed to compare the performance of different methods for checking if an element exists in arrays and sets. The test case includes four scenarios: 1. `Array.includes` 2. `Set.has` 3. `Array.find` (although this method typically returns the found element, not a boolean indicating existence) 4. `Map.has` **What's being tested** The benchmark measures the execution time of each method for checking if an element exists in the specified array or set. **Options Compared** 1. **Array.includes**: This method searches the entire array to find the specified element. 2. **Set.has**: This method uses a hash table to quickly check if an element is present in the set. 3. **Array.find**: Although this method typically returns the found element, the benchmark measures its execution time for checking existence. 4. **Map.has**: Similar to Set.has, this method uses a hash table to quickly check if an element is present in the map. **Pros and Cons** 1. **Array.includes**: * Pros: Easy to implement, intuitive. * Cons: Slow for large arrays, inefficient use of resources. 2. **Set.has**: * Pros: Fast, efficient, suitable for large datasets. * Cons: Requires creating a set, which can be memory-intensive. 3. **Array.find**: * Pros: Returns the found element, easy to implement. * Cons: Slow for checking existence only, inefficient use of resources. 4. **Map.has**: * Pros: Fast, efficient, suitable for large datasets. * Cons: Requires creating a map, which can be memory-intensive. **Library and Special JS Features** 1. `Set` and `Map` are built-in JavaScript objects that provide efficient data structures for storing and retrieving elements. 2. The `Array.find()` method was introduced in ECMAScript 2015 (ES6) and provides a way to find the first element in an array that satisfies a provided condition. **Other Considerations** When choosing between these methods, consider the size of your dataset, the frequency of lookups, and any additional requirements (e.g., returning the found element). Alternative approaches include: 1. **Using `in` operator**: This can be faster than `includes()` for checking existence in arrays. 2. **Using a custom implementation**: Depending on the specific use case, you might be able to optimize the lookup process using a custom algorithm or data structure. Keep in mind that these alternatives may not be as straightforward to implement as the methods tested by the benchmark.
Related benchmarks:
convert to set + set.has vs. array.includes
Includes (array) vs Has (Set)
set.has vs. array.includes vs array.find vs map.has
set vs array find if exists
Comments
Confirm delete:
Do you really want to delete benchmark?