Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Case Insensitive RegEx.test vs. String.includes
(version: 0)
Comparing performance of:
RegEx.test vs String.includes
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var string = "Hello world!"; var regex = /hello/i;
Tests:
RegEx.test
regex.test(string);
String.includes
string.toLowerCase().includes("hello");
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
RegEx.test
String.includes
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
10 months ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36
Browser/OS:
Chrome 138 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
RegEx.test
31204696.0 Ops/sec
String.includes
101731776.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down what's being tested in the provided benchmark. **Benchmark Overview** The benchmark compares two approaches for case-insensitive string matching: using the `test()` method of a regular expression object (`regex.test`) and using the `includes()` method with `toLowerCase()` transformation on the string. **What are we testing?** We're comparing the execution performance of these two approaches: 1. `regex.test(string);`: This method tests if the entire string matches the regular expression pattern, ignoring case. The regex object is created beforehand (`var regex = /hello/i;`). 2. `string.toLowerCase().includes("hello");`: This approach converts the input string to lowercase using `toLowerCase()` and then uses the `includes()` method to search for the substring "hello" in the converted string. **Options Compared** The two options being compared are: 1. **Regex.test**: Using a regular expression object's built-in `test()` method. 2. **String.includes with T.toLowerCase()**: Converting the input string to lowercase and using the `includes()` method. **Pros and Cons of Each Approach:** 1. **Regex.test**: * Pros: + Efficient for simple pattern matching (i.e., "hello" in a short regex). + Often faster than other approaches due to compiler optimizations. * Cons: + Can be slower for complex patterns or large strings. + Requires creating a regex object beforehand, which may incur overhead. 2. **String.includes with T.toLowerCase()**: * Pros: + Simple and easy to implement. + Fast for short substrings in larger strings (due to optimized `includes()` implementation). * Cons: + Requires additional string processing (converting to lowercase) and potential cache misses. **Library and Purpose:** In this benchmark, the regex library is used implicitly by JavaScript's built-in regular expression engine. The purpose of using a regex object (`regex`) is to define a pattern that can be matched against input strings. **Special JS Feature or Syntax:** There are no specific JavaScript features or syntaxes being tested in this benchmark, other than those inherent to the `test()` and `includes()` methods themselves. **Other Alternatives:** If not using regex, some alternative approaches for case-insensitive string matching could include: 1. Using a different string search algorithm, like `indexOf()` with a similar approach. 2. Implementing a custom string comparison function that converts inputs to lowercase or uses a case-insensitive flag. 3. Utilizing a third-party library or module providing optimized string matching functions. However, for most use cases, the built-in `test()` and `includes()` methods are likely sufficient and efficient enough, making alternative approaches less necessary. Keep in mind that benchmarking results may vary depending on specific JavaScript engines, browser versions, and system configurations.
Related benchmarks:
RegEx.test vs. String.includes case insensitive
RegEx.test vs. String.includes vs. String.match in case insensitive scenarios
Case insensitive RegEx.test vs. String.includes when string doesn’t match
regex vs includes - case insensitive
Comments
Confirm delete:
Do you really want to delete benchmark?