Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Case-insensitive string comparison: regex -i versus string.toUppercase
(version: 0)
Comparing performance of:
regex-short vs upper-short vs lower-short vs regex-long vs lower-long vs upper-long
Created:
4 years ago
by:
Guest
Jump to the latest result
Tests:
regex-short
const alpha = "Alpha"; const upperAlpha = "ALPHA"; const lowerAlpha = 'alpha'; const regexAlpha = new RegExp(/alpha/i); var output; output = regexAlpha.test(alpha); output = regexAlpha.test(lowerAlpha); output = regexAlpha.test(upperAlpha);
upper-short
const alpha = "Alpha"; const upperAlpha = "ALPHA"; const lowerAlpha = 'alpha'; const regexAlpha = new RegExp(/alpha/i); var output; output = alpha.toUpperCase() === upperAlpha; output = alpha.toUpperCase() === upperAlpha; output = alpha.toUpperCase() === upperAlpha;
lower-short
const alpha = "Alpha"; const upperAlpha = "ALPHA"; const lowerAlpha = 'alpha'; const regexAlpha = new RegExp(/alpha/i); var output; output = alpha.toLowerCase() === lowerAlpha; output = alpha.toLowerCase() === lowerAlpha; output = alpha.toLowerCase() === lowerAlpha;
regex-long
const longString = `The licenses for most software and other practical works are designed to take away your freedom to share and change the works. By contrast, the GNU General Public License is intended to guarantee your freedom to share and change all versions of a program--to make sure it remains free software for all its users. We, the Free Software Foundation, use the GNU General Public License for most of our software; it applies also to any other work released this way by its authors. You can apply it to your programs, too.`; const lowerLongString = longString.toLowerCase(); const upperLongString = longString.toUpperCase(); const regexLong = new RegExp(/The licenses for most software and other practical works are designed to take away your freedom to share and change the works. By contrast, the GNU General Public License is intended to guarantee your freedom to share and change all versions of a program--to make sure it remains free software for all its users. We, the Free Software Foundation, use the GNU General Public License for most of our software; it applies also to any other work released this way by its authors. You can apply it to your programs, too./i); var output; output = regexLong.test(longString); output = regexLong.test(lowerLongString); output = regexLong.test(upperLongString);
lower-long
const longString = `The licenses for most software and other practical works are designed to take away your freedom to share and change the works. By contrast, the GNU General Public License is intended to guarantee your freedom to share and change all versions of a program--to make sure it remains free software for all its users. We, the Free Software Foundation, use the GNU General Public License for most of our software; it applies also to any other work released this way by its authors. You can apply it to your programs, too.`; const lowerLongString = longString.toLowerCase(); const upperLongString = longString.toUpperCase(); const regexLong = new RegExp(/The licenses for most software and other practical works are designed to take away your freedom to share and change the works. By contrast, the GNU General Public License is intended to guarantee your freedom to share and change all versions of a program--to make sure it remains free software for all its users. We, the Free Software Foundation, use the GNU General Public License for most of our software; it applies also to any other work released this way by its authors. You can apply it to your programs, too./i); var output; output = longString.toLowerCase() === lowerLongString; output = longString.toLowerCase() === lowerLongString; output = longString.toLowerCase() === lowerLongString;
upper-long
const longString = `The licenses for most software and other practical works are designed to take away your freedom to share and change the works. By contrast, the GNU General Public License is intended to guarantee your freedom to share and change all versions of a program--to make sure it remains free software for all its users. We, the Free Software Foundation, use the GNU General Public License for most of our software; it applies also to any other work released this way by its authors. You can apply it to your programs, too.`; const lowerLongString = longString.toLowerCase(); const upperLongString = longString.toUpperCase(); const regexLong = new RegExp(/The licenses for most software and other practical works are designed to take away your freedom to share and change the works. By contrast, the GNU General Public License is intended to guarantee your freedom to share and change all versions of a program--to make sure it remains free software for all its users. We, the Free Software Foundation, use the GNU General Public License for most of our software; it applies also to any other work released this way by its authors. You can apply it to your programs, too./i); var output; output = longString.toUpperCase() === upperLongString; output = longString.toUpperCase() === upperLongString; output = longString.toUpperCase() === upperLongString;
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (6)
Previous results
Fork
Test case name
Result
regex-short
upper-short
lower-short
regex-long
lower-long
upper-long
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Based on the provided benchmark results, I'll analyze the performance of each test. The tests are categorized into three groups: `lower-short`, `upper-short`, and `regex-short`. Each group contains two similar benchmarks (`lower` and `upper`) with slightly different inputs (short strings vs. longer strings). Here's a summary of the key findings: 1. **Lower Short**: The average execution time is approximately 5058179 seconds. This suggests that string comparison operations are relatively fast. 2. **Upper Short**: The average execution time is around 3954213 seconds, indicating that case-insensitive matching (using `toLowerCase()`) might be slower than directly comparing uppercase strings. 3. **Regex Short**: The average execution time is approximately 1533983 seconds, which is significantly higher than the previous two tests. This suggests that regex operations are computationally expensive. Now, let's look at the longer string benchmarks: 1. **Lower Long**: The average execution time is around 1508668 seconds. Although slower than the short strings, it's still relatively fast. 2. **Upper Long**: The average execution time is approximately 762779 seconds, which is faster than `regex-short`. 3. **Regex Long**: This test has a much higher average execution time (approximately 5161324.5 seconds) compared to all previous tests. In summary: * String comparison operations are relatively fast. * Case-insensitive matching using `toLowerCase()` might be slower than direct comparisons for short strings. * Regex operations are significantly more computationally expensive, even when dealing with longer strings. To further optimize the code, I would recommend: * Using a case-insensitive matching approach when possible (e.g., using `toLowerCase()`) to reduce the overhead of direct string comparisons. * Avoiding regex operations whenever feasible due to their high computational cost. * Considering more efficient data structures or algorithms for handling large amounts of strings.
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
Case Insensitive RegEx.test vs. String.includes
regex vs includes - case insensitive
Comments
Confirm delete:
Do you really want to delete benchmark?