Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
regex speed test
(version: 0)
regex speed test
Comparing performance of:
regex vs loop
Created:
4 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script> var input_string = "passw)odas4gsdfsdf"; const regex_test = new RegExp('(?=.*[0-9])'); </script>
Script Preparation code:
var input_string = "passw)odas4gsdfsdf"; const regex_test = new RegExp('(?=.*[0-9])');
Tests:
regex
let isPasswordValid = false; isPasswordValid = regex_test.test(input_string);
loop
let isPasswordValid = false; for (let i = 0; i < input_string.length; i++) { let code = input_string.charCodeAt(i); if (code >= 48 && code <= 57) { isPasswordValid = true; break; } }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
regex
loop
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):
I'd be happy to explain what's being tested in this benchmark. The provided JSON represents a JavaScript microbenchmark that compares the performance of two approaches: using a regular expression (`regex`) and iterating through the input string using a `for` loop (`loop`). Both approaches aim to validate whether a given input string contains at least one digit. **What is being tested?** In essence, this benchmark tests how efficiently each approach can iterate over the input string and check if it contains digits. The test cases are designed to measure the performance difference between these two approaches in terms of the number of executions per second on a specific browser (Firefox 112) running on a Mac OS X 10.15 device. **Options compared** The benchmark compares two main options: 1. **Regular Expression (`regex`)**: Using a regular expression to match any digit within the input string. 2. **Manual Loop (`loop`)**: Iterating through each character of the input string and checking if it's a digit using `charCodeAt()`. **Pros and Cons of each approach** **Regular Expression (`regex`):** Pros: * Concise and easy to read * Can be reused for similar validation tasks Cons: * May have performance overhead due to regex engine processing * Limited control over the matching process (e.g., no way to specify exact digit positions) **Manual Loop (`loop`):** Pros: * Provides direct access to each character's ASCII code value, allowing fine-grained control * Can be optimized for specific use cases or performance needs Cons: * More verbose and harder to read compared to regex * May require more manual testing and debugging effort **Other considerations** * The benchmark uses a relatively simple input string (`"passw)odas4gsdfsdf"`), which may not accurately represent real-world scenarios. * The benchmark focuses on detecting the presence of digits, but other validation tasks (e.g., checking for specific patterns or ranges) might require different approaches. **Library and special JS feature** The `RegExp` class is used to create a regular expression object. There are no other notable libraries or features being used in this benchmark. If you're interested in exploring alternative approaches or testing other JavaScript features, consider the following: * Other regex-related tests could involve exploring the performance of specific regex patterns (e.g., `^` character class, `\d` escape sequence) or using regex flags (e.g., `g`, `m`) to optimize performance. * For loops and array methods (e.g., `forEach()`, `map()`) can be used as alternatives to manual loops for iterating over arrays.
Related benchmarks:
Alphanumeric
regex speed test-2
new RegExp test
new RegExp test 2
Comments
Confirm delete:
Do you really want to delete benchmark?