Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
IndexOf vs Includes vs lodash includes (string)
(version: 0)
IndexOf vs Includes vs lodash includes (string)
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 = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras ultricies accumsan quam, quis blandit sapien vestibulum sed. Donec vulputate faucibus gravida. Proin fermentum commodo orci eget ornare. Morbi elementum eleifend odio, non imperdiet mauris vulputate quis. Fusce tristique vehicula purus eget auctor. Praesent ut dictum risus, quis placerat arcu. In hac habitasse platea dictumst."
Tests:
IndexOf
array.indexOf('ultricies') !== 1
Includes
array.includes('ultricies')
lodash
_.includes(array, 'ultricies')
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 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/135.0.0.0 Safari/537.36 Edg/135.0.0.0
Browser/OS:
Chrome 135 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
IndexOf
121947608.0 Ops/sec
Includes
118822208.0 Ops/sec
lodash
61025420.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the benchmark and its options. **Overview** The benchmark measures the performance of three different approaches to check if a specific string exists in an array: 1. `array.indexOf('ultricies') !== 1` ( native JavaScript ) 2. `array.includes('ultricies')` ( native JavaScript ) 3. `_.includes(array, 'ultricies')` ( Lodash library ) **Native JavaScript Options** The first two options use native JavaScript methods: * `indexOf()` and `includes()`: These methods are part of the standard JavaScript API. The `indexOf()` method searches for a specific value in an array, returning the index of the first occurrence if found, or -1 if not found. The `includes()` method is similar, but it returns a boolean value indicating whether the specified value exists in the array. **Pros and Cons** * **Pros**: + Fast execution: These methods are optimized for performance. + Wide compatibility: They work across most browsers and environments. * **Cons**: + Limited control: The `indexOf()` method returns the index of the first occurrence, while the `includes()` method only returns a boolean value. This might lead to inconsistencies in certain scenarios. **Lodash Option** The third option uses the Lodash library: * `_.includes(array, 'ultricies')`: This method is part of the Lodash utility library. It takes an array and a value as arguments and returns a boolean value indicating whether the specified value exists in the array. **Pros and Cons** * **Pros**: + Easier to use: The Lodash method provides a simple, concise API for checking if a value exists in an array. + More control: The `_.includes()` method returns a boolean value, providing more consistent results than native JavaScript methods. * **Cons**: + Additional dependency: The benchmark requires the Lodash library to be loaded. This might add overhead or affect performance. **Other Considerations** It's worth noting that modern browsers and environments often implement optimizations for these methods, such as: * `indexOf()` method is optimized for performance on modern browsers. * `includes()` method is available in ECMAScript 2020 (ES2020) and later standards. **Alternative Approaches** If you need more control or flexibility, consider using other approaches, such as: * Using a custom implementation with fine-grained control over iteration or indexing. * Utilizing alternative libraries or frameworks that offer similar functionality. * Leveraging Web Assembly (WASM) or other low-level optimization techniques for performance-critical code. In summary, the benchmark allows users to compare the performance of three different approaches: native JavaScript methods (`indexOf()` and `includes()`) and a Lodash library function (`_.includes()`). The choice between these options depends on factors like performance requirements, compatibility, and desired level of control.
Related benchmarks:
lodash some vs native array some
lodash.uniq vs native Set (multi array)
RegEx.test vs. String.includes vs. String.match vs String.indexOf (~2000 characters)
Split join vs replace long string long string
IndexOf vs Includes in string - larger string edition
Comments
Confirm delete:
Do you really want to delete benchmark?