Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
RegEx.test vs. String.includes vs. String.match vs String.indexOf - Long String
(version: 0)
Comparing performance of:
RegEx.test vs String.includes vs String.match vs String.indexOf
Created:
one year ago
by:
Guest
Jump to the latest result
Script Preparation code:
var string = "Lorem ipsum dolor sit amet consectetur adipisicing elit. Ipsam aliquid architecto earum eveniet, commodi nesciunt officia enim, veritatis fugiat ea cumque ipsum, obcaecati deleniti necessitatibus magnam cupiditate consequatur numquam neque ducimus fugit aut nemo eligendi. Odit, cumque! Nisi nemo, tempore magni ad consequatur facere praesentium velit autem assumenda ipsum commodi aliquid nulla labore iste rerum blanditiis est sed enim aut obcaecati vitae. Sunt esse voluptates eaque facere tempora! Nemo corporis harum reprehenderit, placeat error incidunt, esse blanditiis doloremque accusantium adipisci alias fugiat. Aut et dolor ad, nihil obcaecati modi recusandae sed ipsa esse, placeat tempora hic, facilis nobis. Inventore, nemo."; var regex = /architecto/;
Tests:
RegEx.test
regex.test(string);
String.includes
string.includes("architecto");
String.match
string.match("architecto");
String.indexOf
string.indexOf("architecto");
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
RegEx.test
String.includes
String.match
String.indexOf
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36 Edg/128.0.0.0
Browser/OS:
Chrome 128 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
RegEx.test
8693168.0 Ops/sec
String.includes
28768680.0 Ops/sec
String.match
6363811.5 Ops/sec
String.indexOf
24242802.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the benchmark and explain what's being tested. **Benchmark Overview** The benchmark is designed to compare the performance of three different string manipulation methods: 1. `string.includes()` 2. `string.indexOf()` 3. `regex.test()` (using a regular expression) All tests are run on a long, randomly generated string that contains the substring "architecto". **String Manipulation Methods** Each test case uses one of these three methods to search for the substring: * `string.includes(substring)`: This method checks if the string includes the specified substring. * `string.indexOf(substring)`: This method returns the index of the first occurrence of the substring in the string. If the substring is not found, it returns -1. * `regex.test(string)`: This method uses a regular expression to search for the pattern in the string. In this case, the pattern is "/architecto/". **Library Used** The `regex.test()` function uses the built-in JavaScript `RegExp` object, which provides support for regular expressions in JavaScript. **Pros and Cons of Each Approach** * `string.includes()`: This method is simple and efficient but may not be suitable for searching for a specific pattern or substring. + Pros: Fast, easy to use, and doesn't require regular expression syntax. + Cons: May return false positives if the substring is part of another sequence of characters. * `string.indexOf()`: This method is also fast and can provide accurate results but may not be suitable for searching for a specific pattern or substring. + Pros: Fast, provides an index into the string, and doesn't require regular expression syntax. + Cons: May return -1 if the substring is not found, and it's slower than `includes()` for some cases. * `regex.test()`: This method is more powerful and flexible but can be slower due to the overhead of compiling regular expressions. + Pros: Provides precise search results, supports pattern matching, and is useful for searching for specific patterns or substrings. + Cons: Can be slower than the other two methods, requires regular expression syntax. **Other Considerations** * The benchmark only compares these three methods and doesn't consider other string manipulation techniques, such as `string.replace()`, `string.split()`, or `string.split()` with a callback function. * The benchmark uses a fixed-length string that contains the substring "architecto". This may not be representative of real-world use cases, where strings can vary greatly in length and content. **Alternatives** If you wanted to add more tests to this benchmark, you could consider adding tests for other string manipulation methods, such as: * `string.replace()`: Tests replacing a substring with another value. * `string.split()`: Tests splitting a string into an array of substrings. * `string.match()` and `string.exec()`: Tests using regular expressions with the `match()` or `exec()` methods. You could also consider testing different types of strings, such as: * Strings with whitespace characters * Strings containing special characters * Strings encoded in non-ASCII character sets (e.g., UTF-8) Keep in mind that adding more tests can increase the complexity and duration of the benchmark.
Related benchmarks:
replaceAll vs regex replace v2
RegEx.test vs. String.includes vs. String.match (long)
String.replace(RegEx) vs String.replaceAll(String)
IndexOf vs Includes in string - larger string edition
Comments
Confirm delete:
Do you really want to delete benchmark?