Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
IndexOf vs Includes vs lodash includes string
(version: 0)
Banana
Comparing performance of:
IndexOf vs Includes vs lodash
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 = 'sfahsdyfgaydsughausdfgshfuhsud.xlsx';
Tests:
IndexOf
array.indexOf('xlsx') !== 1
Includes
array.includes('xlsx')
lodash
_.includes(array, 'xlsx')
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
IndexOf
Includes
lodash
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's being tested in the provided JSON. **Overview** The benchmark is comparing three different approaches to check if a substring exists in an array: `indexOf`, `includes`, and `_.includes` from the Lodash library. **Approaches compared** 1. **`array.indexOf('xlsx') !== 1`**: This approach uses the `indexOf()` method, which returns the index of the first occurrence of the specified value. If no match is found, it returns -1. By checking if the result is not equal to 1 (the length of the array), we can determine if the substring exists. 2. **`array.includes('xlsx')`**: This approach uses the `includes()` method, which returns a boolean indicating whether the specified value is present in the array or not. 3. **`_.includes(array, 'xlsx')`**: This approach uses the `includes()` function from Lodash, which is similar to the native JavaScript method. **Pros and Cons** 1. **`indexOf`**: * Pros: Lightweight, simple to implement. * Cons: Returns the index of the first occurrence, not a boolean indicating presence/absence. This can lead to incorrect results if the substring appears multiple times in the array. 2. **`includes`** (native JavaScript): * Pros: Simple, efficient, and accurate. * Cons: Only available in modern browsers and Node.js versions. Older browsers may not support it. 3. **`.includes()` from Lodash**: * Pros: Wide browser compatibility, consistent behavior across different browsers. * Cons: Adds an additional dependency (Lodash), which may incur a performance overhead due to the library's complexity. **Other considerations** * The benchmark uses a fixed array with a specific string (`'xlsx'`) as input. This might not represent real-world scenarios, where the input data can vary greatly. * The test doesn't account for cases where the substring is not found in the array (e.g., `array.indexOf('non-existent')`). * The benchmark uses Chrome 103 and Windows 10 as the reference browser/OS. **Alternative approaches** 1. **`toString()` method**: Instead of using `indexOf()` or `includes()`, you can convert the array to a string using `toString()` and then check if the substring is present using the `includes()` method. 2. **`Array.prototype.some()`**: Another approach would be to use the `some()` method, which returns a boolean indicating whether at least one element in the array satisfies the provided callback function. These alternative approaches might provide different results or behaviors compared to the original benchmark. I hope this explanation helps!
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?