Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
string.includes vs string.indexOf vs regex.test(string)
(version: 0)
Comparing performance of:
indexOf vs includes vs regex
Created:
4 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')
includes
string.includes('ullamco')
regex
/ullamco/.test(string)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
indexOf
includes
regex
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/122.0.0.0 Safari/537.36
Browser/OS:
Chrome 122 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
indexOf
32534130.0 Ops/sec
includes
32319680.0 Ops/sec
regex
9236062.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what's being tested. **What is being tested?** The benchmark measures the performance of three string search methods: 1. `string.indexOf()` 2. `string.includes()` 3. Regular Expressions (`/regex.test(string)/`) These methods are compared to determine which one is the fastest. **Options comparison** Here's a brief overview of each option and their pros and cons: 1. **`string.indexOf()`**: * Pros: Simple, widely supported, and efficient for small strings. * Cons: Can be slower for large strings, as it has to search through the entire string from the start. 2. **`string.includes()`**: * Pros: Faster than `indexOf()` for large strings, as it can stop searching once it finds a match. * Cons: May be slower for very small strings due to its algorithmic overhead. 3. **Regular Expressions (`/regex.test(string)/`)**: * Pros: Can be faster than the above two methods, especially for regular expressions with a large number of alternatives. * Cons: Requires an additional library (in this case, none is specified), can be slower for very small strings due to its complexity. **Library usage** None of the tests explicitly use a library for regular expressions. However, if a library were used, it would likely provide additional features and optimizations for the `regex.test()` method. **Special JS feature or syntax** No special JavaScript features or syntax are being tested in this benchmark. **Other alternatives** If you wanted to test other string search methods, here are some alternatives: * **`string.search()`**: Similar to `indexOf()`, but returns -1 if no match is found instead of returning null. * **`String.prototype.indexOf()`**: A shorthand method for calling `indexOf()` on a string instance (e.g., `string.indexOf('ullamco')`). * **NativeWebAssembly**: Not applicable in this benchmark, as it requires specific hardware and software configurations. **Benchmark preparation code** The provided script preparation code creates a large string variable `string`, which is used for all test cases. The HTML preparation code is empty, suggesting that no HTML-related operations are being performed during the benchmarking process. I hope this explanation helps!
Related benchmarks:
Case insensitive RegEx.test vs. String.includes when string doesn’t match
RegEx.test vs. String.includes incasesensitive
Case Insensitive RegEx.test vs. String.includes
regex vs includes - case insensitive
Comments
Confirm delete:
Do you really want to delete benchmark?