Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
string.indexOf boolean vs string.includes
(version: 0)
Comparing performance of:
indexOf vs includes
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var string = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.'
Tests:
indexOf
string.indexOf('ullamco') !== -1
includes
string.includes('ullamco')
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
indexOf
includes
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):
I'd be happy to help you understand what's being tested in this benchmark. **Overview** The benchmark is designed to compare the performance of two string manipulation methods: `string.indexOf` and `string.includes`. These methods are used to search for a specified substring within a given string. **Options Compared** There are only two options being compared: 1. `string.indexOf`: This method returns the index of the first occurrence of the specified substring, or -1 if it's not found. 2. `string.includes`: This method returns a boolean value indicating whether the specified substring is present in the original string. **Pros and Cons** * `string.indexOf`: + Pros: Can be more efficient for searching for specific substrings, as it returns an index instead of a boolean value. This can lead to better cache locality and potentially faster execution. + Cons: If the substring is not found, it returns -1, which may cause issues in some cases. Additionally, if the string is very large, this method may be slower due to the indexing process. * `string.includes`: + Pros: Returns a boolean value immediately, making it more suitable for use cases where you need to quickly determine whether a substring is present or not. + Cons: May be slower than `indexOf` for searching specific substrings, as it needs to iterate through the entire string. **Library and Purpose** The only library used in this benchmark is `string`. The `string` object provides various methods for manipulating strings, including `indexOf` and `includes`. **Special JS Feature or Syntax (None)** There are no special JavaScript features or syntaxes being tested in this benchmark. Both methods are standard string manipulation techniques. **Other Alternatives** If you need to search for a specific substring within a larger string, other alternatives might include: * Using a regular expression with the `RegExp.test` method. * Utilizing a library like Lodash's `includes` function or underscore.js's `contains` function. * Implementing your own custom searching algorithm using iteration and conditional checks. **Benchmark Preparation Code** The provided Script Preparation Code creates a sample string variable `string` containing a long piece of text, which is used as the input for both the `indexOf` and `includes` methods. The Html Preparation Code section is empty, suggesting that HTML-related preparation or execution is not part of this benchmark. I hope this explanation helps you understand what's being tested in this JavaScript microbenchmark!
Related benchmarks:
IndexOf vs Includes in Larger string
IndexOf vs Includes in string with check
Javascript index vs substring
Javascript: Case insensitive string comparison performance 3
RegEx.test vs. String.includes vs. String.match vs String.indexOf - Long String
Comments
Confirm delete:
Do you really want to delete benchmark?