Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Fastest way to do string includes string (ignoring locale)
(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.toLowerCase().indexOf('long') !== -1
includes
myString.toLowerCase().includes('long')
regex case sensi
myString.toLowerCase().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
16244643.0 Ops/sec
includes
16124331.0 Ops/sec
regex case sensi
5621777.5 Ops/sec
regex case insensi
6331179.5 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the explanation of what's being tested in this benchmark. **What is being tested?** The benchmark measures the performance of three different string searching methods: 1. `.indexOf()` 2. `includes()` 3. `match()` with and without case sensitivity (i.e., using a regular expression with an "i" flag for case-insensitive matching) These tests are designed to find the fastest way to search for a specific substring within a large, prepared string. **Options compared:** * `.indexOf()`: This method returns the index of the first occurrence of the specified substring. If the substring is not found, it returns -1. * `includes()`: This method returns a boolean value indicating whether the specified substring exists in the string. * `match()` with and without case sensitivity: The "i" flag is used to make the regular expression case-insensitive. **Pros and cons of each approach:** * `.indexOf()`: + Pros: Simple, efficient, and widely supported. It's a good choice for searching for exact matches. + Cons: Returns -1 if the substring is not found, which might be considered falsey in some contexts. * `includes()`: + Pros: More expressive than `.indexOf()`, as it returns a boolean value instead of an index. + Cons: Might be slower than `.indexOf()` for large strings, since it needs to iterate over the entire string. * `match()` with and without case sensitivity: + Pros: Flexible, as you can use different flags (e.g., "g" for global matching or "u" for Unicode) depending on your needs. + Cons: Can be slower than `.indexOf()` due to the overhead of regular expression evaluation. **Library usage:** None of the benchmark tests rely on any external libraries. The prepared string and regular expressions are hardcoded in the script preparation code. **Special JS features or syntax:** * None of the benchmark tests use any special JavaScript features or syntax that's not widely supported. **Other alternatives:** If you wanted to test these benchmarks differently, here are some alternative approaches: * Use a different search algorithm, such as binary searching or hashing. * Test with different string lengths or character sets (e.g., Unicode characters). * Include additional performance metrics, such as memory usage or garbage collection overhead. * Compare the results with other JavaScript engines or implementations. Keep in mind that these alternatives might not be directly comparable to the original benchmark tests, and you'd need to modify the test cases accordingly.
Related benchmarks:
RegEx.test vs. String.includes vs. String.match vs String.search
RegEx.test vs. String.includes vs. String.match vs String.match(regex) for starting string
RegEx.test vs. String.includes vs. String.match vs == .slice
RegEx.test vs. String.includes vs. String.match vs String.startsWith vs String.localeCompare vs String ===
Long regex test vs string includes
Comments
Confirm delete:
Do you really want to delete benchmark?