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
gemma2:9b
, generated one year ago):
This benchmark compares the performance of three methods for checking if a string ('mx') exists within a data structure: * **`includes()`:** A method available on JavaScript arrays (`a`), it efficiently checks for the presence of a specific value within the array and returns `true` if found, `false` otherwise. This is generally considered the most readable and modern approach. * **`has()`:** A method available on JavaScript Sets (`b`). It checks if a specific value exists within the Set and returns `true` if found, `false` otherwise. Sets are designed for fast lookups, as they don't allow duplicates. * **`indexOf()`:** A method available on JavaScript arrays (`a`), it returns the *index* of the first occurrence of a given value within an array. If not found, it returns -1. In this benchmark, the code checks if `indexOf()` returns a value greater than or equal to 0, effectively simulating a presence check. This method can be slower than `includes()`. **Pros and Cons:** * **`includes()`:** * **Pros:** Readable, efficient, widely supported. * **Cons:** Might be slightly slower than `has()` in some scenarios because it iterates through the array to find the value. * **`has()`:** * **Pros:** Very efficient for membership checking due to the nature of Sets. * **Cons:** Requires creating a Set, which incurs a slight overhead compared to using an existing array directly. * **`indexOf()`:** * **Pros:** Flexible (can return the index), widely supported. * **Cons:** Can be slower than `includes()` and `has()` for simple presence checks because it iterates through the entire array. **Other Considerations:** * **Data Structure Choice:** The choice between an array (`a`) or a Set (`b`) depends on your use case. If you need to avoid duplicate values, a Set is more appropriate. If order matters, an array is better suited. * **Readability:** `includes()` generally offers the most readable and concise solution for membership checks in arrays. **Alternatives:** While this benchmark focuses on these three methods, other approaches exist depending on your specific needs: * **Regex:** For more complex pattern matching, regular expressions could be used. However, they are generally less efficient than the methods compared here for simple string presence checks. * **Map:** If you need to associate values with keys, a Map might be a better choice than an array or Set.
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?