Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array.include vs Set.has Object
(version: 5)
Comparing performance of:
Includes vs Some vs Set
Created:
2 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var array = [] for(i=0; i<150; i++){ array.push({id: i}); } var set = new Set(array)
Tests:
Includes
array.includes(array[0]) array.includes(array[25]) array.includes(array[50]) array.includes(array[100]) array.includes(array[149]) array.includes(array[10000])
Some
array.some(i => i.id === 0) array.some(i => i.id === 25) array.some(i => i.id === 50) array.some(i => i.id === 100) array.some(i => i.id === 149) array.some(i => i.id === 10000)
Set
set.has(0) set.has(25) set.has(50) set.has(100) set.has(149) set.has(10000)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Includes
Some
Set
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**Benchmark Overview** The provided benchmark compares the performance of three different approaches to check for the existence of an element in a large array and a set data structure: `Array.includes()`, `Array.some()` with a callback function, and `Set.has()`. The test cases measure the execution time for each approach. **Options Compared** 1. **Array.includes()**: This method checks if a specified element is present in the array. 2. **Array.some() with callback function**: This method executes a provided callback function on each element of the array; if any element returns `true`, it stops executing and returns `true`. 3. **Set.has()**: This method checks if an element is present in the set. **Pros and Cons** 1. **Array.includes()**: * Pros: Simple, efficient, and widely supported. * Cons: Can be slower for large arrays due to iteration overhead. 2. **Array.some() with callback function**: * Pros: Allows for more flexibility, as you can perform multiple checks or use different types of data structures. * Cons: Typically slower than `Array.includes()` due to the additional overhead of callback execution. 3. **Set.has()**: * Pros: Efficient and fast for checking membership in a set. * Cons: May not be suitable for arrays, as sets are unordered collections. **Library and Purpose** In this benchmark, no specific library is used beyond what's built into the JavaScript standard library (e.g., `Array.includes()` and `Set.has()`). However, `some()` relies on the callback function feature of JavaScript, which allows you to pass a custom function to check for certain conditions. **Special JS Feature or Syntax** None are mentioned in this specific benchmark. However, some other interesting features include: * Callback functions in `Array.some()`, which can be used to perform more complex checks. * The use of `Set.has()` as an efficient alternative to checking membership in a set. **Alternatives** If you're interested in exploring alternatives to these approaches, consider the following options: 1. **Using other array methods**: Other array methods like `Array.indexOf()`, `Array.findIndex()`, or `Array.findIndex()` can be used for similar operations. 2. **Using `Map` data structure**: `Map` provides fast lookups and can be used as an alternative to sets when you need a key-value association. 3. **Native implementations**: Some browsers have native implementations of certain array or set methods (e.g., WebAssembly's `Set` implementation). These might offer better performance than the standard library implementations. In conclusion, this benchmark provides a useful comparison of three different approaches for checking membership in arrays and sets. By understanding the pros and cons of each approach, you can choose the best method for your specific use case.
Related benchmarks:
set vs array includes
JS includes vs set
set.has vs. array.includes (1 million entries)
set vs array includestratsatsrats
new set vs array includes
Comments
Confirm delete:
Do you really want to delete benchmark?