Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
regex speed test-2
(version: 0)
regex speed test
Comparing performance of:
regex vs loop vs loop-2
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; } }
loop-2
let isPasswordValid = false; for (let i = 0; i < input_string.length; i++) { let code = input_string[i]; if (code >= 48 && code <= 57) { isPasswordValid = true; break; } }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
regex
loop
loop-2
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):
The provided JSON represents a JavaScript benchmark test case for measuring the performance of regular expressions versus traditional loop-based methods in checking if a string contains digits. **Benchmark Definition:** The benchmark is defined by a single script that tests whether a given input string contains at least one digit. The script uses two approaches: 1. **Regex Approach:** This approach utilizes a regular expression (`regex_test`) to match any character in the input string (`input_string`) that is equivalent to a digit (i.e., between 48 and 57, inclusive). 2. **Loop-Based Approach:** This approach iterates through each character of the input string using a `for` loop and checks if the character's ASCII code falls within the range of digits (48-57). **Options Compared:** The benchmark compares the performance of two approaches: * Regex Approach * Loop-Based Approach **Pros and Cons of Each Approach:** 1. **Regex Approach:** * Pros: + Concise and expressive syntax. + Can handle complex pattern matching. * Cons: + May have slower performance due to the overhead of regular expression compilation and execution. 2. **Loop-Based Approach:** * Pros: + Typically faster performance since it involves a simple iteration through the string. * Cons: + More verbose and less expressive syntax. **Library Used:** None, as this benchmark does not rely on any external libraries or frameworks. **Special JS Feature/Syntax:** The regular expression used in the benchmark (`(?=.*[0-9])`) is a positive lookahead assertion. This syntax allows the regex engine to check if the current position in the string matches the pattern without actually matching it, which can improve performance by reducing backtracking. **Other Considerations:** When writing benchmarks like this one, consider the following: * Ensure that the benchmark script is representative of real-world usage scenarios. * Use a suitable set of input strings to test both the speed and accuracy of each approach. * Consider using profiling tools or hardware resources to accurately measure performance. **Alternatives:** If you're looking for alternative approaches or benchmarks, consider the following: * [jsperf](https://jsperf.com/): A popular benchmarking tool specifically designed for JavaScript performance testing. * [Benchmark.js](https://benchmarkjs.com/): A cross-browser benchmarking library that supports various benchmark scenarios, including regex and loop-based methods. * [Google's Benchmarking Library](https://github.com/google/benchmark): A high-performance benchmarking library developed by Google.
Related benchmarks:
regex speed test
new RegExp test
new RegExp test 2
RegEx vs For Loop (Optimized)
Comments
Confirm delete:
Do you really want to delete benchmark?