Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
RegEx.test vs String.includes vs String.indexOf
(version: 0)
Comparing performance of: RegEx.test vs String.includes vs String.indexOf
Comparing performance of:
RegEx.test vs String.includes vs String.indexOf
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var string = "Hello world!"; var regex = /Hello/;
Tests:
RegEx.test
regex.test(string)
String.includes
string.includes("Hello")
String.indexOf
string.indexOf("Hello")
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
RegEx.test
String.includes
String.indexOf
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
6 months ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:143.0) Gecko/20100101 Firefox/143.0
Browser/OS:
Firefox 143 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
RegEx.test
54800636.0 Ops/sec
String.includes
2069665536.0 Ops/sec
String.indexOf
2029623680.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the benchmark. **What is tested?** The provided JSON represents a JavaScript microbenchmark that compares the performance of three different approaches: 1. `regex.test(string)`: This function tests the performance of the `test()` method, which is part of the regular expression (RegExp) API. 2. `string.includes("Hello")`: This function tests the performance of the `includes()` method, which is a string method that checks if a substring exists within another string. 3. `string.indexOf("Hello")`: This function tests the performance of the `indexOf()` method, which is also a string method that returns the index of the first occurrence of a substring. **Options compared** The benchmark compares the performance of these three approaches on different strings: * In "RegEx.test", only one specific regular expression `/Hello/` is used to test the performance of the `test()` method. * In "String.includes", only the string `"Hello"` is used, and it's included within a general-purpose string `"Hello world!"`. * In "String.indexOf", also only the string `"Hello"` is used, but with different indices. **Pros and cons** Here are some pros and cons of each approach: 1. `regex.test(string)`: This approach has a relatively simple and specific regular expression to test. * Pros: Easy to maintain and understand; allows for fine-grained control over the regex pattern. * Cons: May not accurately represent real-world use cases, as the regex pattern is very specific. 2. `string.includes("Hello")`: This approach uses a general-purpose string with a substring `"Hello"`. * Pros: More representative of real-world use cases, where strings are often used to search for substrings. * Cons: May be slower than the `regex.test()` method due to the overhead of searching through the entire string. 3. `string.indexOf("Hello")`: This approach also uses a general-purpose string `"Hello world!"`, but with different indices. * Pros: Similar to the previous one, as it's more representative of real-world use cases. * Cons: May not be as effective in cases where the substring appears multiple times. **Other considerations** The benchmark doesn't account for other factors that might affect performance, such as: * Input size and complexity * Platform differences (e.g., browser, OS) * Additional context or flags passed to each method **Library usage** None of the individual test cases use external libraries, but it's worth noting that if a library were used in one of the benchmarking approaches (e.g., `String.prototype.includes()`), it would likely be a standard JavaScript library function. **Special JS features or syntax** There are no special JS features or syntax mentioned in this benchmark. The test cases focus on basic string methods and regular expressions.
Related benchmarks:
RegEx.test vs. String.includes vs. String.indexOf
RegEx.test vs. String.includes vs. String.match insensitive
RegEx.test (with inline regex) vs. String.includes vs. String.match
Longer regex test vs string includes
Comments
Confirm delete:
Do you really want to delete benchmark?