Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Regex vs Includes in Arrays
(version: 1)
Comparing performance of:
Includes vs Regex vs Dynamic Array Includes
Created:
4 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var words = ['html', 'body', 'head']; var blacklistedResourceDomains = [ 'www.gstatic.com', 'maps.gstatic.com', 'www.google-analytics.com', 'maps.googleapis.com', 'www.google.com', 'stats.g.doubleclick.net', ]; var wordsRegex = /(html|head|body)/; var blacklistedResourceDomainsRegex = /(www\.(gstatic|google|google-analytics)\.com|maps\.(gstatic|googleapis)\.com|stats\.g\.doubleclick\.net)/;
Tests:
Includes
const words1 = ['html', 'body', 'head']; const blacklistedResourceDomains1 = [ 'www.gstatic.com', 'maps.gstatic.com', 'www.google-analytics.com', 'maps.googleapis.com', 'www.google.com', 'stats.g.doubleclick.net', ]; const case1 = words1.includes('p'); const case2 = words1.includes('body'); const case3 = blacklistedResourceDomains1.includes('mysite.com'); const case4 = blacklistedResourceDomains1.includes('www.gstatic.com'); const case5 = blacklistedResourceDomains1.includes('www.google-analytics.com');
Regex
const wordsRegex1 = /(html|head|body)/; const blacklistedResourceDomainsRegex1 = /(www\.(gstatic|google|google-analytics)\.com|maps\.(gstatic|googleapis)\.com|stats\.g\.doubleclick\.net)/; const case1 = wordsRegex1.test('p'); const case2 = wordsRegex1.test('body'); const case3 = blacklistedResourceDomainsRegex.test('mysite.com'); const case4 = blacklistedResourceDomainsRegex1.test('www.gstatic.com'); const case5 = blacklistedResourceDomainsRegex1.test('www.google-analytics.com');
Dynamic Array Includes
const case1 = ['html', 'body', 'head'].includes('p'); const case2 = ['html', 'body', 'head'].includes('body'); const case3 = [ 'www.gstatic.com', 'maps.gstatic.com', 'www.google-analytics.com', 'maps.googleapis.com', 'www.google.com', 'stats.g.doubleclick.net', ].includes('mysite.com'); const case4 = [ 'www.gstatic.com', 'maps.gstatic.com', 'www.google-analytics.com', 'maps.googleapis.com', 'www.google.com', 'stats.g.doubleclick.net', ].includes('www.gstatic.com'); const case5 = [ 'www.gstatic.com', 'maps.gstatic.com', 'www.google-analytics.com', 'maps.googleapis.com', 'www.google.com', 'stats.g.doubleclick.net', ].includes('www.google-analytics.com');
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Includes
Regex
Dynamic Array Includes
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Mobile Safari/537.36
Browser/OS:
Chrome Mobile 120 on Android
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Includes
616529792.0 Ops/sec
Regex
2383184.0 Ops/sec
Dynamic Array Includes
572395520.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'd be happy to break down the provided benchmark and explain what's being tested, compared, and their pros and cons. **Benchmark Definition** The benchmark is defined in JSON format, which includes: 1. **Script Preparation Code**: This code sets up variables `words` and `blacklistedResourceDomains`, both of type arrays. 2. **Html Preparation Code**: This field is empty, indicating that no HTML-related setup is required for this benchmark. **Individual Test Cases** There are three test cases defined in the benchmark: 1. **Includes** * The script checks if the strings 'p' and 'body' exist in the `words` array using the `includes()` method. * Additionally, it checks if 'mysite.com', 'www.gstatic.com', and 'www.google-analytics.com' exist in the `blacklistedResourceDomains` array using the `includes()` method. 2. **Regex** * The script defines two regular expression patterns: `wordsRegex` for matching strings within the `words` array, and `blacklistedResourceDomainsRegex` for matching strings within the `blacklistedResourceDomains` array. * It then uses these regex patterns to test if the strings 'p' and 'body' exist in the arrays using the `test()` method. * Additionally, it tests if the domain names 'mysite.com', 'www.gstatic.com', and 'www.google-analytics.com' match the regex patterns using the `test()` method. 3. **Dynamic Array Includes** * The script checks if the strings 'p' and 'body' exist in the `words` array using the `includes()` method, similar to the "Includes" test case. * However, this time, it uses a literal array instead of defining an array with variable values. * It also checks if the domain names 'mysite.com', 'www.gstatic.com', and 'www.google-analytics.com' exist in the `blacklistedResourceDomains` array using the `includes()` method, similar to the "Includes" test case. **Comparison** The three test cases compare the performance of different approaches: 1. **Includes**: Tests the use of the `includes()` method for array-based string matching. 2. **Regex**: Tests the use of regular expressions for string matching. 3. **Dynamic Array Includes**: Tests a variation of the "Includes" approach using literal arrays. **Pros and Cons** Here's a brief overview of the pros and cons of each approach: 1. **Includes** * Pros: Simple, straightforward, and widely supported. * Cons: May be slower for large datasets or complex string matching. 2. **Regex** * Pros: Flexible and powerful for complex pattern matching. * Cons: Can be slower due to the overhead of compiling regex patterns, and may not be as efficient for simple string matching. 3. **Dynamic Array Includes** * Pros: Similar performance to "Includes", but with a different array initialization approach. * Cons: May require more memory allocation and deallocation for large arrays. **Other Considerations** When choosing between these approaches, consider the specific requirements of your use case: * If you need simple string matching for small datasets, `includes()` might be the best choice. * If you need to match complex patterns or work with large datasets, regex patterns might be more suitable. * If you're concerned about memory allocation and deallocation overhead, using a literal array instead of defining an array with variable values (as in "Dynamic Array Includes") might be a good option. Keep in mind that these are general guidelines, and the actual performance differences may vary depending on your specific use case and environment.
Related benchmarks:
blackList vs regexp2
endsWith vs regex match
String includes space (regex vs String.includes)
String includes space (regex vs String.includes) 2
Comments
Confirm delete:
Do you really want to delete benchmark?