Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
IndexOf vs Includes vs lodash includes vs manual check
(version: 0)
Banana
Comparing performance of:
IndexOf vs Includes vs lodash vs manual check
Created:
5 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js'></script>
Script Preparation code:
var array = ['banana', 'sausage', 'jesus']
Tests:
IndexOf
array.indexOf('sausage') !== 1
Includes
array.includes('sausage')
lodash
_.includes(array, 'sausage')
manual check
(array[0] === 'sausage' || array[1] === 'sausage' || array[2] === 'sausage')
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
IndexOf
Includes
lodash
manual check
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):
Let's break down the benchmark and its individual test cases. **Benchmark Purpose** The benchmark compares the performance of four different approaches to check if an element exists in an array: 1. `array.indexOf('sausage') !== 1`: This method checks for the index of the specified element, returning -1 if not found. 2. `array.includes('sausage')`: This method returns a boolean indicating whether the element is present in the array. 3. `_.includes(array, 'sausage')`: This is a version of the `includes` method provided by the Lodash library. 4. `(array[0] === 'sausage' || array[1] === 'sausage' || array[2] === 'sausage')`: This is a manual implementation that checks each element in the array. **Library Used** The benchmark uses the Lodash library, specifically the `_.includes` method. The library provides additional utility functions for common tasks. **Pros and Cons of Each Approach** 1. `array.indexOf('sausage') !== 1`: * Pros: Simple and intuitive. * Cons: Returns -1 if not found, which might be considered falsey in some contexts. 2. `array.includes('sausage')`: * Pros: Fast and efficient, returning a boolean result. * Cons: Might require additional operations to handle cases where the element is not present. 3. `_.includes(array, 'sausage')` (Lodash): * Pros: Provides a consistent and predictable implementation. * Cons: Requires an additional library dependency. 4. `(array[0] === 'sausage' || array[1] === 'sausage' || array[2] === 'sausage')`: * Pros: Simple and fast, but might be slower for large arrays. * Cons: Less readable and maintainable due to its manual implementation. **Other Considerations** * The benchmark uses a small array with only three elements. As the array size increases, the performance differences between these methods may become more pronounced. * The `includes` method is generally faster than the `indexOf` method because it can stop searching as soon as it finds the element. * The Lodash implementation provides a consistent and predictable result, making it easier to reason about the code. **Benchmark Results** The latest benchmark results show that: 1. `array.includes('sausage')` is the fastest, with approximately 155.36 executions per second (on Chrome 101 on Windows). 2. `_.includes(array, 'sausage')` comes close, with around 8.34 executions per second. 3. The manual implementation `(array[0] === 'sausage' || array[1] === 'sausage' || array[2] === 'sausage')` is slower, with approximately 4.34 executions per second. Keep in mind that these results may vary depending on the specific use case and environment.
Related benchmarks:
IndexOf vs Includes vs lodash includes
IndexOf vs Includes vs lodash includes test2
IndexOf vs Includes vs lodash includes for string
array IndexOf vs array Includes vs lodash indexOf
Comments
Confirm delete:
Do you really want to delete benchmark?