Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Case insensitive search: regex.test() vs string.toLowerCase().includes()
(version: 0)
Comparing performance of:
string.toLowerCase().includes() vs regex.test()
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var testStr = 'QWERtyuioIUYTRE'; var input = 'uioiu'; var inputRegex = new RegExp(input, 'i');
Tests:
string.toLowerCase().includes()
testStr.toLowerCase().includes(input);
regex.test()
inputRegex.test(testStr);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
string.toLowerCase().includes()
regex.test()
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; rv:138.0) Gecko/20100101 Firefox/138.0
Browser/OS:
Firefox 138 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
string.toLowerCase().includes()
1195157120.0 Ops/sec
regex.test()
24316350.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the world of JavaScript microbenchmarks on MeasureThat.net. The provided JSON represents a benchmark that compares two approaches for case-insensitive string searches: using `regex.test()` versus `string.toLowerCase().includes()`. Here's a breakdown of what's tested: **Options being compared:** 1. **Regex test (`inputRegex.test(testStr)`)**: This approach uses a regular expression to perform the search. The `new RegExp` constructor creates a regex object with the specified input pattern (`input`) and flags (in this case, `'i'`, which enables case-insensitive matching). 2. **String `toLowerCase()` + `includes()`**: This approach converts the test string to lowercase using `toLowerCase()` and then uses the `includes()` method to search for the input substring. **Pros and Cons of each approach:** 1. **Regex test (`inputRegex.test(testStr)`)**: * Pros: Can be more efficient for searching a fixed pattern in a large text, as it avoids creating a new string or using a loop. * Cons: May have performance overhead due to the creation of a regex object and the execution of the regular expression engine. Additionally, regex patterns can be complex and harder to read/maintain. 2. **String `toLowerCase()` + `includes()`**: * Pros: Often faster for simple substring searches, as it leverages the optimized implementation of `includes()`. It also avoids creating a new string or using a loop. * Cons: Requires two operations (converting to lowercase and performing the search), which may lead to additional overhead. Other considerations: * The benchmark doesn't account for the impact of input size on performance, which could affect the results. * Both approaches assume that the test string is already parsed and available in memory. If the test string needs to be read from a file or network resource, this would add significant overhead. **Library used:** In this benchmark, no specific library is required or provided beyond the built-in `RegExp` class and JavaScript's standard library (`String.prototype.toLowerCase()` and `String.prototype.includes()`). **Special JS feature/syntax:** This benchmark uses the `includes()` method, which was introduced in ECMAScript 2015 (ES6). It's a convenient and efficient way to search for a substring within a string. Note that this feature is widely supported across modern JavaScript engines. Now, let's look at other alternatives: * **Other approaches**: + Using a loop or indexing to find the substring: `testStr.indexOf(input.toLowerCase())` + Using a library like Sregex or regex-ast for more efficient regular expression matching * **Alternative optimizations**: + Pre-compiling the regular expression or the string conversion + Optimizing the input size and character set for each approach Keep in mind that these alternatives may not be as straightforward to implement or maintain, but they might offer performance improvements in specific scenarios.
Related benchmarks:
case insensitive search
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?