Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
RegEx.test vs. array .includes
(version: 1)
Comparing performance of:
RegEx.test vs String.includes
Created:
one year ago
by:
Guest
Jump to the latest result
Script Preparation code:
var list = ["Hello", 'world', 'hello1', 'world1', 'hello2', 'world2', 'hello3', 'world3']; var regex = /Hello|world|hello1|world1|hello2|world2|hello3|world3/;
Tests:
RegEx.test
regex.test('Hello'); regex.test('hello2'); regex.test('world3');
String.includes
list.includes("Hello"); list.includes("hello2"); list.includes("world3");
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
RegEx.test
String.includes
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
8 months ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/139.0.0.0 Safari/537.36
Browser/OS:
Chrome 139 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
RegEx.test
5321341.0 Ops/sec
String.includes
9121638.0 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated one year ago):
The benchmark provided compares two different approaches for searching for strings in a pre-defined list: using a regular expression (`RegEx.test`) versus using the `Array.includes` method. ### Benchmark Breakdown 1. **Benchmark Preparation**: - A list of strings is defined: `["Hello", 'world', 'hello1', 'world1', 'hello2', 'world2', 'hello3', 'world3']`. - A regular expression is created to match any of these strings: `/Hello|world|hello1|world1|hello2|world2|hello3|world3/`. 2. **Individual Test Cases**: - **Test Case 1: RegEx.test** - This test evaluates the performance of the `RegEx.test` method when applied to three different strings: `'Hello'`, `'hello2'`, and `'world3'`. - The `.test()` method of a regular expression returns `true` if there is a match; otherwise, it returns `false`. - **Test Case 2: String.includes** - This test evaluates the performance of the `Array.includes` method, also using the same three strings. - The `includes()` method checks if a particular value exists in the array, returning `true` or `false`. ### Performance Results - The benchmark results show that `Array.includes` outperforms `RegEx.test`: - `String.includes`: 22,983,580 executions per second - `RegEx.test`: 11,154,954 executions per second ### Pros and Cons #### RegEx.test **Pros**: - Versatile: Can match complex patterns and combinations due to the capabilities of regular expressions. - Powerful: Ideal for more advanced use cases requiring pattern matching (e.g., partial matches, case insensitivity, etc.). **Cons**: - Performance: Slower than simpler string searching methods, especially for straightforward value checks. - Complexity: Regular expressions can be harder to read and understand, particularly for developers who are not well-versed in them. #### String.includes **Pros**: - Simplicity: Very straightforward API; easy to read and understand. - Performance: Generally faster than regular expressions for simple existence checks in arrays. **Cons**: - Limited functionality: Only checks for the presence of an exact string, not allowing for more complex matching scenarios like partial matches. ### Other Considerations - When working with simple string matches, `Array.includes` is often the preferred method due to its clarity and performance. - In cases where you need to find patterns rather than exact matches, using regular expressions might be more appropriate despite the performance overhead. - Depending on the use case (e.g., the size of the dataset, the complexity of the pattern being searched), developers should carefully consider which method to utilize. ### Other Alternatives 1. **Array.find()** or **Array.indexOf()**: These methods can also be used depending on the specific need. `find()` can return the first matching element or `undefined` if no matches exist, while `indexOf()` returns the index of the occurrence or `-1` if not found. 2. **Set**: For scenarios involving multiple searches, utilizing a `Set` may improve lookup performance as it has better time complexity for existence checks compared to arrays. 3. **Custom Search Algorithms**: If specific conditions or large datasets are involved, a custom search algorithm (such as binary search on sorted arrays) might be developed. In summary, this benchmark provides a clear comparison of performance metrics between two commonly used JavaScript methods for string searching, emphasizing the strengths and weaknesses of each approach under the scenario presented.
Related benchmarks:
RegEx.test vs. String.includes vs. String.match
RegEx.test vs. String.includes vs. String.indexOf
Regex check
RegEx.test vs. String.includes vs. String.match v2
RegEx.test vs. String.includes vs. String.match (multiple words in regex)
RegEx.test vs. String.includes vs. String.match vs String.match(regex)
RegEx.test vs. String.includes vs. String.match vs Array.includes
RegEx.test vs. String.includes vs. String.match1
RegEx.test vs. String.includes x 2
Comments
Confirm delete:
Do you really want to delete benchmark?