Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
RegEx.test vs Array.includes Email cashing test:
(version: 1)
Comparing performance of:
RegEx.test vs Array.includes
Created:
one year ago
by:
Guest
Jump to the latest result
Script Preparation code:
var string = "lue-ka@gmail.com"; var array = [string, 'ewerwqerqwr', ''] var regex = /^(?!\.|\s)(?!.*\.\.)[\p{L}\d._%+-]+?(?<![\.+(&])@[\p{L}\d]+(?:[\p{L}\d-]+)*(?<!-)(?:\.[\p{L}\d]+(?:[\p{L}\d-]+)*(?<!-))*(\.[a-zA-Z]{2,5})$/u;
Tests:
RegEx.test
regex.test(string);
Array.includes
array.includes(string);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
RegEx.test
Array.includes
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:133.0) Gecko/20100101 Firefox/133.0
Browser/OS:
Firefox 133 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
RegEx.test
7206402.5 Ops/sec
Array.includes
41507480.0 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated one year ago):
The benchmark defined in this JSON document compares the performance of two different methods to check for the presence of a specific valid email string in a set of options: **regular expression matching** using `RegExp.test()` and **array inclusion checking** using `Array.includes()`. ### Comparison of Approaches 1. **RegEx.test()** - **Purpose**: This method uses a regular expression to validate if the specified string adheres to the email pattern defined by the regex. - **Pros**: - Can validate the structure of an email and check if it matches the regex pattern. This is essential for determining if a string is not only present but correctly formatted as an email. - **Cons**: - Typically slower because regex evaluation can be computationally intensive, especially for complex patterns. - Can potentially lead to performance issues if not designed carefully, particularly with poorly constructed regular expressions (e.g., excessive backtracking). 2. **Array.includes()** - **Purpose**: This method checks whether a provided array contains a specific element (the email string in this case). - **Pros**: - Fast and efficient for simply checking the existence of an item in an array. - Easier to read and understand at a glance compared to regex, especially for simple membership tests. - **Cons**: - Does not check whether the string is a valid email format. It simply evaluates if the string exists in the array. - Limited to static matching (exact match) without performing any pattern validation. ### Benchmark Results The benchmark results indicate that **Array.includes()** significantly outperforms **RegEx.test()**, with a measured execution rate of approximately **41,507,480 executions per second** compared to about **7,206,402 executions per second** for the regex method. This stark difference highlights the performance implications when choosing the appropriate method for string checks. ### Considerations in Choosing Between the Two 1. **Use Case**: If you need to validate the structure of an email (checking format), use RegExp.test(). However, if you only need to verify presence among known valid strings in an array, use Array.includes(). 2. **Performance Criticality**: In scenarios where high performance is essential (such as in loops or large datasets), prefer Array.includes() for simple presence checks. 3. **Complexity vs Simplicity**: Regular expressions can introduce complexity; therefore, for straightforward tasks, relying on simpler methods can enhance maintainability. ### Alternatives to the Methods Tested - **String.indexOf()**: This method can also be used to check if the email is present in an array by converting the array to a string. It is less preferred in terms of clarity than Array.includes(). - **Set Data Structure**: If multiple checks are required on a set of email addresses, using a `Set` for membership testing can improve performance further over an array due to average O(1) time complexity for checks. - **Library-Based Approaches**: Libraries like **Lodash** provide utility functions (e.g., `_.includes()`) that can enhance code clarity and maintainability while also optimizing performance under certain situations. In conclusion, while both methods serve important roles, the choice between regex-based validation and array membership checks should be informed by specific use cases, performance requirements, and clarity of code.
Related benchmarks:
Chilinh --> RegEx.test vs. String.includes vs. String.match vs String.IndexOf
My Check - RegEx.test vs. String.includes vs. String.match vs String.IndexOf
check regex vs split
check regex vs split2
check regex vs split3
check regex vs split4
search @ in email
RegEx.exec vs String.test
RegEx.test vs Array.forEach Array.find Email cashing test:
Comments
Confirm delete:
Do you really want to delete benchmark?