Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Fastest way to do string includes string
(version: 0)
Comparing performance of:
.indexOf vs includes vs regex case sensi vs regex case insensi
Created:
2 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
window.myString = 'a looooooong string who knows how Long' window.regexpI = new RegExp('long', 'i'); window.regexp = new RegExp('long');
Tests:
.indexOf
myString.toLocaleLowerCase().indexOf('long') !== -1
includes
myString.toLocaleLowerCase().includes('long')
regex case sensi
myString.toLocaleLowerCase().match(regexp)
regex case insensi
myString.match(regexpI)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
.indexOf
includes
regex case sensi
regex case insensi
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36
Browser/OS:
Chrome 121 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
.indexOf
12517119.0 Ops/sec
includes
12475868.0 Ops/sec
regex case sensi
5527187.0 Ops/sec
regex case insensi
6327572.5 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the world of JavaScript microbenchmarks. **Benchmark Definition** The benchmark measures the fastest way to perform string includes (e.g., `includes()`, `indexOf()`). The script preparation code sets up two variables: `myString` and two regular expressions, `regexpI` and `regexp`. The HTML preparation code is empty. **Options Compared** Three options are compared: 1. `.includes()`: This method returns a boolean value indicating whether the string contains the specified value. 2. `.indexOf()`: This method returns the index of the first occurrence of the specified value in the string, or -1 if not found. 3. Using regular expressions with `match()` (with and without case sensitivity). **Pros and Cons** Here's a brief summary of each option: * `.includes()`: + Pros: Simple, easy to read, and well-documented. + Cons: May be slower than other methods due to the overhead of creating a boolean value. * `.indexOf()`: + Pros: Returns an index, which can be useful for finding the position of a substring in a string. However, it's not always necessary. + Cons: May return -1 if the substring is not found, which could lead to unexpected behavior. * Regular expressions with `match()`: + Pros: Can match complex patterns and has built-in support for case insensitivity (`i` flag). + Cons: Can be slower than other methods due to the overhead of regular expression compilation. **Special JS Features** The test uses a special JavaScript feature called "regex case sensitivity" (denoted by the `i` flag in the `RegExp` constructor). The `i` flag makes the regular expression engine perform a case-insensitive match. There is no other special JavaScript feature mentioned in this benchmark. **Library Usage** There are no libraries used in this benchmark. **Other Considerations** When choosing between these methods, consider the following: * If you need to check if a string contains a substring, `.includes()` might be the simplest and most efficient choice. * If you need to find the position of a substring in a string, `.indexOf()` might be a better option. * If you need to match complex patterns or work with case-insensitive matches, regular expressions with `match()` might be the way to go. **Alternatives** Other alternatives for this benchmark could include: * Using the `String.prototype.includes()` method (available in modern browsers) instead of `.includes()` * Using the `String.prototype.indexOf()` method (available in modern browsers) instead of `.indexOf()` * Adding more complex regular expressions or using other string manipulation methods like `replace()` or `split()` Keep in mind that the best approach will depend on the specific requirements and constraints of your use case.
Related benchmarks:
String.Split and String.Match
String vs regex
RegEx.test vs. String.includes vs. String.match vs == .slice
startsWith or regex test
match vs split multiple test string
Comments
Confirm delete:
Do you really want to delete benchmark?