Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Javascript: Case insensitive string comparison (regex vs includes)
(version: 1)
Comparing performance of:
toLowerCase() vs Regex vs toLowerCase() FAIL vs Regex FAIL
Created:
one year ago
by:
Guest
Jump to the latest result
Script Preparation code:
var a = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras blandit mauris sed bibendum pulvinar. Morbi vel velit a sapien cursus blandit sit amet vitae massa. Curabitur eleifend est justo, sed porttitor nunc euismod id. Sed tincidunt nisi felis, consectetur eleifend augue condimentum et. Integer id felis dolor. Orci varius natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Vivamus suscipit, purus sit amet feugiat feugiat, ipsum nisl laoreet ex, et ultricies nisi eros sit amet turpis. Integer vitae velit vel nulla gravida tempus vel eu nunc. Sed vitae consectetur orci, non varius sem. Donec fringilla eros eu lacus tristique, at facilisis mi zzzzzzz.Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras blandit mauris sed bibendum pulvinar. Morbi vel velit a sapien cursus blandit sit amet vitae massa. Curabitur eleifend est justo, sed porttitor nunc euismod id. Sed tincidunt nisi felis, consectetur eleifend augue condimentum et. Integer id felis dolor. Orci varius natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Vivamus suscipit, purus sit amet feugiat feugiat, ipsum nisl laoreet ex, et ultricies nisi eros sit amet turpis. Integer vitae velit vel nulla gravida tempus vel eu nunc. Sed vitae consectetur orci, non varius sem. Donec fringilla eros eu lacus tristique, at facilisis mi zzzzzzz.Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras blandit mauris sed bibendum pulvinar. Morbi vel velit a sapien cursus blandit sit amet vitae massa. Curabitur eleifend est justo, sed porttitor nunc euismod id. Sed tincidunt nisi felis, consectetur eleifend augue condimentum et. Integer id felis dolor. Orci varius natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Vivamus suscipit, purus sit amet feugiat feugiat, ipsum nisl laoreet ex, et ultricies nisi eros sit amet turpis. Integer vitae velit vel nulla gravida tempus vel eu nunc. Sed vitae consectetur orci, non varius sem. Donec fringilla eros eu lacus tristique, at facilisis mi finibus." var b = "finibus" var c = "zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz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zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz"
Tests:
toLowerCase()
a.toLowerCase().includes(b.toLowerCase())
Regex
const re = new RegExp(b, "gi") re.test(a)
toLowerCase() FAIL
c.toLowerCase().includes(b.toLowerCase())
Regex FAIL
const re = new RegExp(b, "gi") re.test(c)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
toLowerCase()
Regex
toLowerCase() FAIL
Regex FAIL
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
Browser/OS:
Chrome 131 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
toLowerCase()
2533561.5 Ops/sec
Regex
2848745.0 Ops/sec
toLowerCase() FAIL
3028573.8 Ops/sec
Regex FAIL
4992538.5 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated one year ago):
In the provided benchmark, the focus is on comparing the performance of two different approaches for case-insensitive string comparison in JavaScript. The two main methodologies being compared are: 1. **Using String Methods**: - **Method**: `a.toLowerCase().includes(b.toLowerCase())` - **Test Name**: "toLowerCase()" - **Description**: This approach converts both the main string (`a`) and the target string (`b`) to lowercase before checking if `b` is contained within `a`. 2. **Using Regular Expressions**: - **Method**: `const re = new RegExp(b, "gi"); re.test(a)` - **Test Name**: "Regex" - **Description**: This method creates a regular expression with the `g` (global) and `i` (ignore case) flags to test if the string `b` exists within `a`. In addition to these main comparisons, there are considerations for "failure" cases where the main string (`c`), which is largely filled with repetitive data, is used instead of `a`. The tests `toLowerCase() FAIL` and `Regex FAIL` evaluate how these methods perform when checking for the presence of `b` within a string that does not logically contain it. ### Performance Results: 1. **Regex FAIL**: Executed approximately 4,992,538.5 times per second. 2. **toLowerCase() FAIL**: Executed approximately 3,028,573.75 times per second. 3. **Regex**: Executed approximately 2,848,745.0 times per second. 4. **toLowerCase()**: Executed approximately 2,533,561.5 times per second. ### Pros and Cons of Each Approach: - **Using `toLowerCase()`**: - **Pros**: - Easier to understand and straightforward for less complex cases. - Reliable as it makes no assumptions about the content of the string. - **Cons**: - The transformation to lowercase can be costly in terms of performance, especially with larger strings since it creates new string instances. - Requires two separate method calls. - **Using Regular Expressions**: - **Pros**: - Generally faster in cases where the regular expression engine performs well, as seen in some of the benchmark results. - Tends to be more efficient when matching patterns rather than just substring searches. - **Cons**: - Can be harder to read and understand if one is not well-versed in regex syntax. - There are overhead costs with creating the regular expression object, and performance can vary significantly based on the regex complexity and context. ### Other Considerations: - **Usage Context**: The context of use may heavily influence which method performs better for a specific use case. For example, if the comparison is very frequent and with very large strings, performance implications could lead to a preference for the regex approach. - **Length of the Strings**: The length of `a` and `b` can significantly impact performance, and these benchmarks utilize a particularly large string (`a`) to highlight this. ### Alternatives: There are other means to achieve case-insensitive comparisons in JavaScript, such as: - **Locale-Sensitive Comparisons**: Using `localeCompare()` to compare strings based on specific locales and case handling. - **String Normalization**: Using `normalize()` combined with string comparison, especially for strings containing special characters. These alternatives cater to different needs, like ensuring adherence to specific locale rules or handling complex characters effectively. Ultimately, the choice of method will depend on the specific requirements of performance, readability, support for internationalization, and the specific nature of the content being handled.
Related benchmarks:
regex vs includes speed comparison
regex vs includes javascript comparison
String Test indexOf vs Search
Reduce w/ Lowercase vs. Magic Regex
Javascript index vs substring
RegEx.test vs. String.includes vs. String.match (long)
RegEx.test vs. String.includes vs. String.match vs String.indexOf (~2000 characters)
Javascript: Case insensitive string comparison performance 3
replaceAll no regex vs replace with regex
Comments
Confirm delete:
Do you really want to delete benchmark?