Toggle navigation
MeasureThat
.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Case insensitive RegEx.test vs. String.includes when string doesn’t match
(version: 0)
Comparing performance of:
RegEx.test vs String.includes
Created:
3 years ago
by:
Guest
Go to the latest result
Script Preparation code:
var string = "Hello world!"; var regex = /Hello1/i;
Tests:
RegEx.test
regex.test(string);
String.includes
string.toLowerCase().includes("Hello1");
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:
4 months ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/145.0.0.0 Safari/537.36
Browser/OS:
Chrome 145 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
String.includes
42.1M/s
RegEx.test
31.9M/s
View exact numbers
Test name
Executions per second
✓
String.includes
42,054,628 Ops/sec
RegEx.test
31,884,546 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what's being tested. **Benchmark Overview** The benchmark compares two approaches for case-insensitive regular expression matching: `regex.test()` and `string.toLowerCase().includes()`. The goal is to determine which approach is faster, especially when a string does not match the regex pattern. **Test Cases** There are two test cases: 1. **RegEx.test**: This test case uses a regular expression (`/Hello1/i`) to search for a substring (`"Hello1"`) in a given string (`"Hello world!"`). The `/i` flag makes the matching case-insensitive. 2. **String.includes**: This test case converts the input string to lowercase using `string.toLowerCase()` and then checks if the lowercase version of the regex pattern (`"Hello1"`) is included in the original string. **Options Compared** The two approaches are compared in terms of performance (executions per second). * **RegEx.test()**: This approach uses a built-in JavaScript method for regular expression testing, which likely employs an optimized algorithm and engine. * **string.toLowerCase().includes()**: This approach uses the `includes()` method on a string that has been converted to lowercase. The `includes()` method is a more general-purpose string searching function. **Pros and Cons** Here's a brief analysis of each approach: * **RegEx.test()**: + Pros: Typically faster and more efficient for regular expression matching. + Cons: May require additional memory allocation and processing power if the regex pattern is complex. * **string.toLowerCase().includes()**: + Pros: Often simpler to implement and may be more suitable for simple string searching tasks. + Cons: Can be slower due to the need to convert the input string to lowercase, which can lead to increased memory usage. **Library** In this benchmark, `regex` is a built-in JavaScript object that provides methods for working with regular expressions. The `String.includes()` method is also a built-in JavaScript method for checking if a substring exists within another string. **Special JS Feature/Syntax** There are no special JavaScript features or syntax used in this benchmark. However, it's worth noting that the `/i` flag in the RegEx pattern enables case-insensitive matching. **Other Alternatives** Other approaches to regular expression matching or string searching might include: * Using a dedicated library like `regex-escape` or `regex-utils` * Implementing a custom regex engine * Using a different string searching algorithm, such as a Boyer-Moore search Keep in mind that these alternatives may not be relevant to the specific use case of this benchmark.
Related benchmarks
RegEx.test vs. String.includes case insensitive
Case Insensitive RegEx.test vs. String.includes
regex vs includes - case insensitive
Comments
Confirm delete:
Do you really want to delete benchmark?