Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
RegEx.test vs. String.includes vs. String.match (long)
(version: 0)
Comparing performance of:
RegEx.test vs String.includes vs String.match
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var string = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed vitae dolor orci. Suspendisse a elit eu lectus semper varius quis non quam. Nullam non turpis sit amet ipsum interdum vehicula. Suspendisse potenti. Nulla egestas turpis in ante eleifend, mattis pharetra nisi porta. Morbi a eros risus. Pellentesque mi neque, eleifend non augue nec, dictum tincidunt erat. Etiam vel lacus ut quam porta ullamcorper. Sed molestie turpis ipsum, in vulputate dui condimentum vitae. Donec facilisis facilisis tellus, in pretium tellus ullamcorper vel. Cras semper dictum nulla non rhoncus. Nam condimentum faucibus tempus. Donec lectus quam, luctus sed molestie id, gravida non tortor. Sed ac turpis sit amet erat aliquam vestibulum."; var regex = /pretium/;
Tests:
RegEx.test
regex.test(string);
String.includes
string.includes("pretium");
String.match
string.match("pretium");
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.match
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36 Edg/131.0.0.0
Browser/OS:
Chrome 131 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
RegEx.test
5568958.0 Ops/sec
String.includes
79763944.0 Ops/sec
String.match
2935856.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'll break down the provided benchmark and explain what's being tested, compared options, pros and cons of each approach, and other considerations. **Benchmark Test Case** The benchmark test case measures the performance of three different methods for searching a specific string within another string: 1. `RegEx.test(string)`: This method uses a regular expression to search for a pattern in the input string. 2. `string.includes("pretium")`: This method checks if the specified substring is present anywhere in the input string. 3. `string.match("pretium")`: This method searches for a specific pattern in the input string and returns an array of matches or null. **Library Used** In this benchmark, none of the methods use a dedicated library. However, regular expressions (`RegEx.test`) are part of the JavaScript standard library. **Special JS Features/ Syntax** None of the methods rely on special JavaScript features like async/await, generators, or promises. **Comparison and Options** The three methods compare the performance of different approaches for searching substrings within strings: * `RegEx.test(string)`: This method uses a regular expression engine to search for a pattern in the input string. It's more efficient than the other two methods but may be slower due to the overhead of creating a regular expression object. * `string.includes("pretium")`: This method checks if the specified substring is present anywhere in the input string using a simple loop. It's generally faster and more memory-efficient than the other two methods but may not perform well for larger substrings or strings. * `string.match("pretium")`: This method searches for a specific pattern in the input string using a regular expression engine. Like `RegEx.test`, it's more efficient than `includes` but may be slower due to the overhead of creating a regular expression object. **Pros and Cons** Here are some pros and cons of each approach: * `RegEx.test(string)`: Pros: + More efficient for searching patterns in strings + Can be faster than `string.includes` or `string.match` Cons: + May require more memory to create a regular expression object + Can be slower due to the overhead of creating a regex object * `string.includes("pretium")`: Pros: + Faster and more memory-efficient than `RegEx.test` + Simple and easy to understand Cons: + May not perform well for larger substrings or strings + Less efficient than `RegEx.test` for searching patterns * `string.match("pretium")`: Pros: + More efficient than `string.includes` for searching patterns + Can be faster than `RegEx.test` Cons: + May require more memory to create a regular expression object + Can be slower due to the overhead of creating a regex object **Other Considerations** When choosing between these methods, consider the following: * If you need to search for specific patterns in strings, use `RegEx.test` or `string.match`. * If you only need to check if a substring is present in a string, use `string.includes`. * If memory efficiency is critical and speed is not a top priority, use `string.includes`. * If performance is critical and memory efficiency is not a concern, use `RegEx.test` or `string.match`. **Alternatives** Other alternatives for searching substrings within strings include: * Using the `indexOf()` method with a specific substring * Using the `split()` method to split the string into an array of substrings * Using a dedicated library like `regex-exec` or `regex-perf` However, these alternatives may not provide the same level of performance as the original three methods.
Related benchmarks:
regex vs includes speed comparison
String Test indexOf vs Search
RegEx.test vs. String.includes vs. String.match vs String.indexOf (~2000 characters)
String.replace(RegEx) vs String.replaceAll(String)
Comments
Confirm delete:
Do you really want to delete benchmark?