Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
String toLowerCase vs. Regex test
(version: 0)
Different methods of checking whether a string contains upper-case letters
Comparing performance of:
toLowerCase vs RegExp
Created:
3 years ago
by:
Guest
Jump to the latest result
Tests:
toLowerCase
const strings = [ 'abcd', 'ABCD', 'Abcd', 'defg', 'defG', '1234', 'blah', 'nine', 'TEN', ]; strings.forEach((el) => { el.toLowerCase() === el; });
RegExp
const strings = [ 'abcd', 'ABCD', 'Abcd', 'defg', 'defG', '1234', 'blah', 'nine', 'TEN', ]; strings.forEach((el) => { /[A-Z]/.test(el); });
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
toLowerCase
RegExp
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
7 months ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:144.0) Gecko/20100101 Firefox/144.0
Browser/OS:
Firefox 144 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
toLowerCase
54067336.0 Ops/sec
RegExp
6502918.5 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**Overview of the Benchmark** The provided JSON represents a JavaScript benchmark that compares two approaches for checking whether a string contains upper-case letters: the `toLowerCase()` method and regular expressions (RegEx). **Approach 1: `toLowerCase()` Method** The first approach uses the `toLowerCase()` method, which converts a string to lowercase. This method is implemented in the browser's built-in JavaScript engine. **Pros of using the `toLowerCase()` method:** * Fast execution speed, as it is optimized for performance. * Simple and straightforward implementation. * No additional dependencies or libraries are required. **Cons of using the `toLowerCase()` method:** * May not be suitable for strings that contain non-ASCII characters (e.g., accented letters). * May not provide the desired level of case insensitivity, as it only converts to lowercase. **Approach 2: Regular Expressions (RegEx)** The second approach uses RegEx to match uppercase letters in a string. This method is implemented using a regular expression engine that is part of the browser's JavaScript engine or a separate library. **Pros of using RegEx:** * Provides case-insensitive matching, allowing for accurate detection of upper-case letters. * Can be used with non-ASCII characters, making it more versatile than the `toLowerCase()` method. **Cons of using RegEx:** * May be slower than the `toLowerCase()` method due to the overhead of compiling and executing regular expressions. * Requires additional dependencies or libraries (e.g., the ` RegExp` object). * Can be more complex to implement, especially for developers without experience with regular expressions. **Library Used in the Benchmark** In this benchmark, the `RegExp` object is used to implement the RegEx approach. The `RegExp` constructor takes two arguments: a pattern and an optional flags parameter. In this case, the pattern `[A-Z]` matches any uppercase letter. **Special JS Feature or Syntax** There is no special JavaScript feature or syntax used in this benchmark beyond regular expressions. However, it's worth noting that modern JavaScript engines often provide additional features and optimizations for specific use cases, such as string manipulation. **Other Alternatives** If you're interested in exploring other approaches for checking whether a string contains upper-case letters, here are some alternatives: * Using a custom implementation with bitwise operations (e.g., using `charCodeAt()` and `>>> 0`). * Using a library like Lodash or Underscore.js that provides utility functions for working with strings. * Using a different data structure, such as an array of uppercase letters, to check for presence. Keep in mind that these alternatives may offer trade-offs in terms of performance, readability, and maintainability.
Related benchmarks:
case insensitive search
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?