Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Regex vs String.includes in Array.filter v2
(version: 0)
Comparing performance of:
Regex match vs String includes with toLowerCase
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var array = ["PHV Versicherung", "Rücktrittsversicherung", "Lebensversicherung"] var searchStrings = ["rücktritt", "versicher", "rung", "P"]
Tests:
Regex match
for(let i = 0; i < searchStrings; i++) { array.filter(str => { const rgx = new RegExp(searchStrings[i], "i"); return str.match(rgx); }) }
String includes with toLowerCase
for(let i = 0; i < searchStrings; i++) { array.filter(str => { return str.toLowerCase().includes(searchStrings[i].toLowerCase()); }) }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Regex match
String includes with toLowerCase
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
11 months ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/137.0.0.0 Safari/537.36
Browser/OS:
Chrome 137 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Regex match
1366281.9 Ops/sec
String includes with toLowerCase
973803.1 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided JSON and explain what's being tested. **Benchmark Overview** The benchmark is designed to compare two approaches: 1. **Regular Expressions (Regex)**: This approach uses the `RegExp` constructor to create a regular expression pattern, which is then used to search for matches in an array of strings. 2. **String `includes()` method**: This approach uses the `includes()` method to check if a string contains a specific substring. **Options Compared** The benchmark compares the performance of these two approaches on an array of strings that contain various substrings, including "rücktritt", "versicher", "rung", and "P". **Pros and Cons of Each Approach** 1. **Regular Expressions (Regex)**: * Pros: + Can be more flexible than the `includes()` method, as it can match patterns with variables and quantifiers. + Can be used to extract data from strings that don't contain the expected substring. * Cons: + Can be slower than the `includes()` method for simple string searches. + Requires more memory and CPU resources to compile the regular expression pattern. 2. **String `includes()` method**: * Pros: + Faster than the Regex approach, especially for small strings or simple searches. + Simpler to implement and less prone to errors. * Cons: + Less flexible than Regex, as it only checks for exact substring matches. + May not be suitable for advanced string matching tasks. **Library Used** In this benchmark, no specific library is required. The `RegExp` constructor and the `includes()` method are built-in JavaScript functions. **Special JS Features or Syntax** The benchmark uses the following special features: * **Template literals**: Used in the script preparation code to create a template string for the array initialization. * **Arrow functions**: Used in the benchmark definitions to define small, anonymous functions. **Other Alternatives** If you wanted to use different approaches, here are some alternatives: 1. **String `indexOf()` method**: Instead of using `includes()`, you could use `indexOf()` to find the index of the first occurrence of a substring. 2. **S regex**: You could also use a simple regular expression like `/rücktritt/i` instead of creating a full-fledged RegExp object. 3. **Native JavaScript functions**: Depending on the specific requirements, other native JavaScript functions like `String.prototype.replace()` or `String.prototype.matchAll()` might be more suitable. Keep in mind that the performance differences between these alternatives will depend on the specific use case and hardware configuration.
Related benchmarks:
String includes space (regex vs String.includes) 2
RegEx vs Array.includes
RegEx vs Array.includes v2
regex.test vs. array.includes
Comments
Confirm delete:
Do you really want to delete benchmark?