Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
.includes() vs indexOf() for single-character search in string
(version: 0)
Comparing performance of:
includes() vs indexOf()
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var my_str = "thisStrinWillEventuallyContainASpace see I told you?"
Tests:
includes()
var hasSpace = my_str.includes(' ');
indexOf()
var hasSpace = my_str.indexOf(' ') >= 0;
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
includes()
indexOf()
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64; rv:124.0) Gecko/20100101 Firefox/124.0
Browser/OS:
Firefox 124 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
includes()
1210281472.0 Ops/sec
indexOf()
1341665024.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the benchmark. The provided JSON represents a JavaScript microbenchmark that compares two approaches for searching for a single character in a string: `.includes()` and `indexOf()`. Here's what's being tested: **Approaches being compared** There are two test cases: 1. `.includes()` method, which checks if a specified value (in this case, `' '`) is present in the string (`my_str`). 2. `indexOf()` method, which returns the index of the first occurrence of the specified value (again, `' '`) in the string. **Pros and Cons** * `.includes()`: This method is generally considered more efficient than `indexOf()` because it can handle multiple values simultaneously using a single call. However, for a single character search like this, the performance difference might be negligible. + Pros: More concise and potentially faster for multiple value searches. + Cons: May incur additional overhead for non-existent values or when dealing with large strings. * `indexOf()`: This method returns the index of the first occurrence of the specified value, which can be useful in certain contexts. However, it requires a separate call for each character search, increasing overall execution time. + Pros: More accurate and precise control over indexing. + Cons: Requires multiple calls, potentially slowing down the benchmark. **Library and Special JS Features** In this case, there are no specific libraries being used, but we can assume that `includes()` and `indexOf()` are standard JavaScript methods. No special features or syntax are being tested in this benchmark. **Other Alternatives** If you need to search for a single character in a string, other approaches might include: * Using a regular expression: `/'[space]/` would match a space character and return its index. * Using `split()`: Splitting the string into an array of substrings using a single space character as the delimiter can be an efficient way to search for specific characters. **Benchmark Preparation Code** The provided preparation code creates a sample string `my_str` with a space character, which is used as the target value for both tests. Overall, this benchmark provides a simple yet informative comparison of two common JavaScript methods for searching for single characters in strings.
Related benchmarks:
String indexOf vs includes
Js Search -String IndexOf vs Includes
String.indexOf vs Array split and includes
String.IndeOf vs. String.includes
Comments
Confirm delete:
Do you really want to delete benchmark?