Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
RegEx vs For Loop 2
(version: 0)
-
Comparing performance of:
RegEx vs For Loop vs RegEx pre-compiled
Created:
5 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
var string = "asdFORBIDDENasd";
Script Preparation code:
var string = "asdFORBIDDENasd";
Tests:
RegEx
let isPasswordValid = false; let forbiddenValues = ['test', 'another', 'value', 'other', 'foo', 'bar', 'one', 'two', 'three', 'forbidden'] let regexString = forbiddenValues.join('|') let regex = new RegExp('.*(' + regexString + ').*', 'i'); isPasswordValid = regex.test(string);
For Loop
let forbiddenValues = ['test', 'another', 'value', 'other', 'foo', 'bar', 'one', 'two', 'three', 'forbidden'] for (let val of forbiddenValues) { if (string.toLowerCase().includes(val)) { return false; } }
RegEx pre-compiled
let isPasswordValid = false; const regex = new RegExp(/.*(test|another|value|other|foo|bar|one|two|three|forbidden).*/i); isPasswordValid = regex.test(string);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
RegEx
For Loop
RegEx pre-compiled
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36
Browser/OS:
Chrome 126 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
RegEx
2497135.2 Ops/sec
For Loop
5217650.5 Ops/sec
RegEx pre-compiled
7173980.5 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**Overview of the Benchmark** The provided JSON represents a JavaScript microbenchmark that compares the performance of three approaches to validate a password against a list of forbidden values: 1. Using a regular expression (Regex) with a string literal as the pattern 2. Using a for loop with string matching using `includes()` 3. Pre-compiling the regular expression **Options Compared** The benchmark compares the execution speed of each approach on different browsers and devices. * **RegEx**: The first approach uses a regular expression to validate the password. + Pros: Can be more flexible and powerful than other approaches, but can also be slower due to the complexity of the regex pattern. + Cons: Requires careful crafting of the regex pattern to avoid performance issues or false positives. * **For Loop**: The second approach uses a for loop with string matching using `includes()`. + Pros: Simple and easy to understand, can be faster than Regex for simple cases. + Cons: May become slower as the number of forbidden values increases, due to the iteration over the array. * **RegEx pre-compiled**: The third approach pre-compiles the regular expression before executing it. + Pros: Can improve performance by avoiding the overhead of compiling the regex on each execution. + Cons: Requires creating a compiled version of the regex pattern upfront. **Library and Syntax** In this benchmark, no specific JavaScript libraries are used. However, the `includes()` method is used in the for loop approach, which is a built-in JavaScript method introduced in ECMAScript 2015 (ES6). **Other Considerations** * The benchmark uses Firefox as the browser, but other browsers may have different performance characteristics. * The test cases use a simple password validation scenario, and the results may not generalize to more complex scenarios or edge cases. * The benchmark does not account for other factors that could affect performance, such as cache effects, garbage collection, or concurrent execution. **Alternatives** Other alternatives to the approaches compared in this benchmark include: * Using a more advanced regex pattern or feature, such as lookaheads or word boundaries * Using a different string matching algorithm, such as `indexOf()` or `lastIndexOf()` * Using a dedicated library for password validation, such as bcrypt or scrypt * Using a different programming paradigm, such as functional programming with higher-order functions Overall, the benchmark provides a simple and controlled way to compare the performance of different approaches to password validation in JavaScript.
Related benchmarks:
String.match vs. RegEx.test
RegEx.exec vs String.match
RegEx.exec vs regex.test
RegEx Length vs String Length
.endsWith vs last char check regex
Comments
Confirm delete:
Do you really want to delete benchmark?