Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
IndexOf vs Includes vs lodash includes vs lodash indexOf
(version: 0)
Comparing performance of:
IndexOf vs Includes vs lodash includes vs Lodash indexOf
Created:
6 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
_.includes(array, 'sausage')
Lodash indexOf
_.indexOf(array, 'sausage')
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
IndexOf
Includes
lodash includes
Lodash 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.2:3b
, generated one year ago):
Let's break down what is being tested in the provided JSON and explain each option, its pros and cons, and other considerations. **Overview** The test case measures the performance of three methods to find an element in an array: `indexOf`, `includes`, and two versions using the Lodash library (`_.indexOf` and `_ includes`). The goal is to determine which method is the fastest. **Options being compared** 1. **`array.indexOf('sausage') !== 1`**: This option uses the native JavaScript method `indexOf`. It returns `-1` if the element is not found, and its index if it's found. 2. **`array.includes('sausage')`**: This option uses the native JavaScript method `includes`, which was introduced in ES2019 (Chrome 76). 3. **`_.includes(array, 'sausage')`**: This option uses the Lodash library function `_includes`. It provides a more robust and flexible alternative to the native `includes` method. 4. **`_.indexOf(array, 'sausage')`**: This option uses the Lodash library function `_indexOf`, which is similar to the native `indexOf` method but can handle more edge cases. **Pros and Cons of each approach** 1. **Native `indexOf`**: * Pros: Fastest, simple implementation. * Cons: Only works for arrays, returns `-1` if not found, doesn't handle null or undefined inputs well. 2. **Native `includes`**: * Pros: Faster than native `indexOf`, more robust with null or undefined inputs, works on strings and numbers. * Cons: Only introduced in ES2019, may be less efficient for older browsers. 3. **Lodash `_includes`**: * Pros: More flexible, can handle edge cases like empty arrays or null inputs, provides a fallback implementation if native methods are not available. * Cons: Slower than native `indexOf`, requires Lodash library inclusion. 4. **Lodash `_indexOf`**: * Pros: Similar to native `indexOf`, handles more edge cases, provides a fallback implementation if native method is not available. * Cons: Requires Lodash library inclusion, slower than native `indexOf`. **Other considerations** * The test case uses the Chrome 76 browser, which supports the native `includes` method. This may affect the results for older browsers or environments that don't support this method. * The test cases use an array of strings and a specific element (`'sausage'`) to make the comparison more focused on performance rather than general-purpose functionality. **Alternative approaches** If you want to test other methods or variations, you could consider adding more options, such as: * Using `Array.prototype.find()` or `Array.prototype.findIndex()` instead of `indexOf` and `includes`. * Using a different library or implementation for the Lodash functions. * Adding more complex scenarios, like handling null or undefined inputs, edge cases, or large datasets.
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?