Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
RegEx.test vs. String.includes vs. String.match with lowercase
(version: 0)
Comparing performance of:
RegEx.test vs String.includes vs String.match
Created:
2 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.toLowerCase());
String.includes
string.toLowerCase().includes("hello");
String.match
string.toLowerCase().match("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.match
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
15 days ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/147.0.0.0 Safari/537.36
Browser/OS:
Chrome 147 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
RegEx.test
31735336.0 Ops/sec
String.includes
79800344.0 Ops/sec
String.match
12127472.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the explanation of the provided benchmark. **Benchmark Purpose** The benchmark measures the performance of three different approaches to test if a string contains a certain substring: 1. `String.includes()` 2. `RegExp.test()` with a case-insensitive flag 3. `String.match()` with a pattern that matches the substring These methods are used to find a specific sequence of characters within a larger string. **Options Compared** The benchmark compares the performance of three different options: * `String.includes()`: This method returns `true` if the specified value is found in the string, and `false` otherwise. It uses a linear search algorithm. * `RegExp.test()` with a case-insensitive flag: This method tests if the regular expression matches the entire string, ignoring case differences. The flag is used to make the match case-insensitive. * `String.match()`: This method returns an array of strings if the pattern is found anywhere in the string, or `null` otherwise. It uses a linear search algorithm. **Pros and Cons** Here are some pros and cons of each approach: * `String.includes()`: + Pros: Simple and widely supported. + Cons: Linear search algorithm can be slow for large strings. * `RegExp.test()` with a case-insensitive flag: + Pros: Can match patterns more complex than simple substrings. + Cons: Requires a regular expression, which can add overhead. The case-insensitive flag may not be desirable in all cases. * `String.match()`: Not typically used for substring matching, as it returns an array of matches or `null`. **Library Usage** None of the provided benchmark tests use any external libraries. **Special JavaScript Features/Syntax** The benchmark uses two special JavaScript features: 1. `RegExp` objects: These are used to define regular expressions. 2. `String.toLowerCase()` and `string.toLowerCase()`: These methods convert a string to lowercase, which is used in the benchmark to ensure case-insensitive matching. **Other Alternatives** If you want to compare other approaches to substring matching, here are some alternatives: * `indexOf()` method: This method returns the index of the first occurrence of the specified value, or `-1` if not found. * `includes()`: This method is similar to `indexOf()`, but returns a boolean value instead of an index. Note that these alternatives may have different performance characteristics and trade-offs compared to the methods tested in the benchmark.
Related benchmarks:
RegEx.test vs. String.includes case insensitive
RegEx.test vs. String.includes vs. String.match insensitive
Case insensitive RegEx.test vs. String.includes when string doesn’t match
Case Insensitive RegEx.test vs. String.includes
regex vs includes - case insensitive
Comments
Confirm delete:
Do you really want to delete benchmark?