Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Substring in a string: IndexOf vs Includes vs lodash includes
(version: 0)
Testing for the speed of finding a substring in a 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 text = "1KX5 P/CKD TORTELLINI BRAIS BEEF CA CT 631 ***** note: we don't sell this. *****";
Tests:
IndexOf
text.indexOf('note') !== 1
Includes
text.includes('note')
lodash
_.includes(text, 'note')
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 (Macintosh; Intel Mac OS X 10.15; rv:131.0) Gecko/20100101 Firefox/131.0
Browser/OS:
Firefox 131 on Mac OS X 10.15
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
IndexOf
1562450816.0 Ops/sec
Includes
1558164736.0 Ops/sec
lodash
35513388.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Measuring that net is an excellent resource for comparing the performance of different JavaScript methods. **Benchmark Definition** The provided JSON represents a benchmark test for finding a substring in a string using three different methods: `indexOf`, `includes`, and a custom implementation with Lodash's `includes` function. The test checks which method is faster, providing insights into the performance characteristics of each approach. **Options Compared** 1. **`indexOf`**: This method searches for the specified value in the string and returns the index of the first occurrence. If the value is not found, it returns -1. 2. **`includes`**: This method checks if a string includes another string as a substring. It's similar to `indexOf`, but it doesn't return an index; instead, it returns `true` or `false`. 3. **`lodash.includes`**: This function is part of the Lodash library and performs the same check as `includes`. However, its implementation might be optimized for performance. **Pros and Cons** * **`indexOf`**: Pros: Simple to implement, widely supported. Cons: Returns an index (if found) or -1 if not found, which can lead to unnecessary calculations. * **`includes`**: Pros: Similar to `indexOf`, but returns a boolean value, making it more concise. Cons: Also returns `true` or `false`, similar to `indexOf`. * **`lodash.includes`**: Pros: Optimized for performance, provides a simple and concise API. Cons: Requires the Lodash library, which might add overhead. **Library Usage** In this benchmark, the Lodash library is used for its `includes` function. This suggests that the author wants to compare the performance of the built-in JavaScript methods with a popular utility function from another library. **Special JS Feature/Syntax** There doesn't appear to be any special JavaScript features or syntax mentioned in the provided code snippets. The focus is on comparing different string search methods. **Other Alternatives** If you're interested in exploring alternative approaches, here are some other options: * **`String.prototype.includes()`**: Similar to `includes`, but part of the standard JavaScript API. * **`Array.prototype.indexOf()`**: If you want to find an index within a larger array or string. * **Regular Expressions**: Can be used for more complex searches, such as pattern matching. For further information on measuring performance in JavaScript, consider checking out [Measuring that net](https://www.measuringthat.net/) and other resources.
Related benchmarks:
IndexOf vs Includes -- Strings
lodash _.indexOf vs native indexOf with strings
substring vs indexOf
array indexOf vs includes vs some vs for loop
Comments
Confirm delete:
Do you really want to delete benchmark?