Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
contains vs regex
(version: 0)
Comparing performance of:
list vs regex
Created:
5 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var urls = `/as/ /a/b /c/d /e/f `; var url = 'http://example.com/a/b/cd/e/f'; var list = urls.match(/[^\r\n]+/g); var regex = new RegExp(urls.match(/[^\r\n]+/g).join('|'));
Tests:
list
for(var i=0; i++; i<list.length) { if (url.contains(list[i])) { break; } }
regex
1+1
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
list
regex
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
3 months ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/143.0.0.0 Safari/537.36 Edg/143.0.0.0
Browser/OS:
Chrome 143 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
list
63557048.0 Ops/sec
regex
62763524.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what's being tested, compared options, pros and cons, library usage, special JS features, and other considerations. **Benchmark Description** The benchmark measures the performance difference between two approaches: using `contains()` method on a string (`list[i]`) versus using regular expressions to match strings against a pattern (`regex`). **Comparison Options** 1. **Using `contains()` method**: This approach uses the built-in `contains()` method of JavaScript strings, which is implemented in the browser's engine. 2. **Using Regular Expressions (Regex)**: This approach uses the `new RegExp()` constructor to create a regular expression pattern and then uses the `test()` method to match the string against this pattern. **Pros and Cons** * **Using `contains()` method**: + Pros: Typically faster and more efficient, as it's implemented in the browser's engine. + Cons: May not work correctly for all cases (e.g., edge cases like Unicode characters), especially if the input strings are very large or complex. * **Using Regular Expressions (Regex)**: + Pros: More flexible and powerful than `contains()`, with support for regular expression syntax features like character classes, quantifiers, and anchors. However, this can come at a performance cost. + Cons: Slower and more resource-intensive due to the overhead of creating and executing regular expressions. **Library Usage** There is no explicit library mentioned in the benchmark definition or test cases. However, it's worth noting that ` RegExp` objects are part of the JavaScript Standard Library, which means they're implemented by the browser and don't require an additional library inclusion. **Special JS Features** * **String `contains()` method**: This method is not a standard part of JavaScript. It's likely that this benchmark tests the implementation of the `contains()` method in the specific browser being tested. * **Regular Expressions (Regex)**: Regular expressions are a powerful feature in JavaScript, and their usage in this benchmark demonstrates the need for efficient execution. **Other Considerations** * The benchmark uses a fictional `urls` string to create a list of strings (`list`) that will be matched against a URL (`url`). This setup ensures consistent test inputs. * The benchmark assumes that the input strings are encoded as UTF-8, which is the default encoding for JavaScript strings. **Alternatives** If you wanted to write similar benchmarks using other approaches, here are some alternatives: 1. **Using `indexOf()` method**: Instead of using the `contains()` method or regular expressions, you could use the `indexOf()` method to find the index of a substring within a string. 2. **Using a different regular expression syntax**: You could experiment with alternative regular expression syntax features, such as lookaheads or lookbehinds, to see how they impact performance. 3. **Testing with larger input datasets**: To further test the performance of these approaches, you could increase the size of the input dataset and observe how the results change. Keep in mind that writing benchmarks requires a good understanding of the JavaScript engine's implementation details and the specific features being tested.
Related benchmarks:
split vs regex onurl
Test rgecg
regecxt
regecxgth
Comments
Confirm delete:
Do you really want to delete benchmark?