Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
indexOf vs includes search2
(version: 0)
Comparing performance of:
indexOf vs includes
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var str = "hello world"; var findStr = "world";
Tests:
indexOf
(str.indexOf(findStr) > -1);
includes
str.includes(findStr);
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):
**What is being tested?** MeasureThat.net is testing two different approaches to search for a substring within a string in JavaScript: `indexOf` and `includes`. The benchmark compares the performance of these two methods on the same input data. **Options compared:** 1. **`indexOf`**: This method returns the index of the first occurrence of the specified value, or -1 if it is not found. 2. **`includes`**: This method returns a boolean indicating whether the string includes the specified value. **Pros and Cons of each approach:** * `indexOf`: + Pros: - Can be used to search for the first occurrence of a substring in both forward and backward directions (using `indexOf(str, start, end)`). - Can return an index even if the value is not found. + Cons: - May have performance issues with very long strings due to the need to traverse the string from start to end. - Can be slower than `includes` for short strings or repeated searches. * `includes`: + Pros: - Faster and more efficient than `indexOf`, especially for small strings. - Does not require traversing the entire string like `indexOf`. + Cons: - May have issues with very long strings, as it uses a different algorithm that can be slower. **Library usage:** None of the test cases use any external libraries. The benchmark is only using built-in JavaScript methods. **Special JS feature or syntax:** There are no special features or syntax used in this benchmark. It is purely focused on comparing the performance of two standard JavaScript methods. **Other alternatives:** 1. **`startsWith`**: Another method to search for a substring at the start of a string, which may be faster than `indexOf` for certain use cases. 2. **Regular expressions**: Can be used to search for patterns in strings, but may have performance and complexity issues compared to `indexOf` and `includes`. 3. **Substring extraction libraries**: Specialized libraries can be used for substring extraction, such as `substr` or `substring()`, which may offer better performance than built-in methods. In summary, MeasureThat.net is testing the performance of two standard JavaScript methods: `indexOf` and `includes`. The benchmark highlights the differences in performance between these two approaches, especially for long strings.
Related benchmarks:
Js Search -String IndexOf vs Includes
Js Search - String StartsWith vs Includes
find vs includes vs indexof
.includes() vs indexOf() for single-character search in string
Comments
Confirm delete:
Do you really want to delete benchmark?