Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
IndexOf vs Includes vs lodash includes for string
(version: 0)
Banana
Comparing performance of:
IndexOf vs Includes vs lodash
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 = 'bananasausagejesus'
Tests:
IndexOf
array.indexOf('sausage') !== -1
Includes
array.includes('sausage')
lodash
_.includes(array, 'sausage')
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:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/132.0.0.0 Mobile Safari/537.36
Browser/OS:
Chrome Mobile 132 on Android
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
IndexOf
72100096.0 Ops/sec
Includes
76355080.0 Ops/sec
lodash
49683728.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the benchmark and explain what's being tested. **Benchmark Overview** The benchmark compares three methods for checking if a string exists within an array: 1. `array.indexOf('sausage') !== -1` 2. `array.includes('sausage')` 3. `_.includes(array, 'sausage')` (using the Lodash library) **Options Being Compared** Here's what each option is doing: * **Option 1: `indexOf` method** + This method returns the index of the first occurrence of the specified value (`'sausage'`) in the array. If the value is not found, it returns `-1`. + Pros: simple and widely supported. + Cons: can be slower than other options, especially for large arrays, since it has to iterate through the entire array to find a match. * **Option 2: `includes` method** + This method returns a boolean indicating whether the specified value (`'sausage'`) exists in the array. + Pros: more concise and readable than `indexOf`, and can be faster for large arrays since it uses a optimized implementation. + Cons: not all browsers support this method, so it may not work in older or less modern environments. * **Option 3: `_.includes` method (using Lodash)** + This method returns a boolean indicating whether the specified value (`'sausage'`) exists in the array. + Pros: highly optimized and widely supported, thanks to its use of native methods and ES6 features. + Cons: requires including the Lodash library, which may add overhead for some users. **Library Used** The `_.includes` method is using the Lodash library, which provides a comprehensive set of utility functions for JavaScript. Lodash is widely used in web development and is known for its performance and reliability. **Special JS Feature or Syntax** There are no special JavaScript features or syntaxes being tested in this benchmark. The focus is on comparing three well-established methods for string existence checks. **Other Alternatives** If you're interested in exploring alternative approaches, here are a few options: * **Array.prototype.find**: This method returns the first element of the array that satisfies the provided testing function. * **String.indexOf() + Array.includes()` : This approach combines the `indexOf` method on strings with the `includes` method on arrays. While it may seem like an obvious combination, it's not always the most efficient or readable solution. * **For loop**: You could also use a simple for loop to iterate through the array and check each element individually. However, this approach would likely be slower than using the built-in methods provided by modern JavaScript engines. Overall, the benchmark is providing valuable insights into the performance characteristics of different string existence checks in modern web browsers. By comparing three well-established methods, it's helping users understand the trade-offs between readability, maintainability, and raw performance.
Related benchmarks:
IndexOf vs Includes vs lodash includes
IndexOf vs Includes -- Strings
IndexOf vs Includes vs lodash includes test2
array IndexOf vs array Includes vs lodash indexOf
Comments
Confirm delete:
Do you really want to delete benchmark?