Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
set.has vs. array.includes vs array.indexOf (small set of string values)
(version: 0)
Comparing performance of:
includes vs lookup vs indexof
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var a = [ 'height', 'width', 'mx', 'maxHeight', ]; var b = new Set(a)
Tests:
includes
return a.includes('mx')
lookup
return b.has('mx')
indexof
return a.indexOf('mx') >= 0
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
includes
lookup
indexof
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.1:latest
, generated one year ago):
Let's dive into the explanation. **Benchmark Definition:** The provided JSON represents a benchmark test case that compares the performance of three different JavaScript methods for checking if an element exists in a set or array of values. **Test Cases:** There are three individual test cases: 1. **includes**: This test case uses the `a.includes('mx')` method, where `a` is an array containing string values. 2. **lookup**: This test case uses the `b.has('mx')` method, where `b` is a Set object created from the same array `a`. 3. **indexof**: This test case uses the `a.indexOf('mx') >= 0` method. **Library and Features:** * None specific libraries are used in this benchmark. * However, JavaScript features like Sets (`new Set()`), Array methods (`includes()` and `indexOf()`), and the use of template literals for string formatting are employed. * Note that there's no special JS feature or syntax mentioned in the test cases. **Options Compared:** The three test cases compare different approaches to checking if an element exists in a set or array: 1. **Array.includes()**: This method is designed specifically for searching elements within arrays and returns `true` or `false`. 2. **Set.has()**: This method is part of the Set object, which allows for efficient lookup and insertion operations. It returns `true` if the element exists in the set. 3. **Array.indexOf()**: Although not as efficient as `includes()` for finding elements in an array, this method can also be used to search for elements. **Pros and Cons:** * **includes()**: + Pros: Designed specifically for searching elements within arrays; fast and efficient. + Cons: Not applicable for Sets or other data structures. * **has()** (Set): + Pros: Efficient lookup operation with an average time complexity of O(1). + Cons: Only suitable for Sets, not for arrays. * **indexOf()**: + Pros: Can be used for both arrays and Strings; returns the index if found, or -1. + Cons: Not as efficient as `includes()` for large arrays. **Other Considerations:** * The results show that the `includes()` method is significantly faster than the other two methods in this specific benchmark. * The performance difference may vary depending on the size of the array, the number of elements to search for, and the browser or environment being used. **Alternatives:** For similar use cases: 1. Use `Array.prototype.includes()` when working with arrays or strings. 2. Utilize Sets (`new Set()`) when you need efficient lookup operations, especially when dealing with large data sets. 3. Employ other data structures like Maps (key-value pairs) for more complex scenarios. Keep in mind that the best approach often depends on specific requirements and constraints of your project.
Related benchmarks:
convert to set + set.has vs. array.includes
Small n set vs array
set vs array find if exists
set vs array find if exists v2
Array includes vs Set.has
Comments
Confirm delete:
Do you really want to delete benchmark?