Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
regex vs string functions3
(version: 0)
Comparing performance of:
domain 1 vs domain 2
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
function domain1(x, wild = 'n', subs = 'o') { return x.length < 256 && new RegExp( `^(?=(` + // capture (wild === 'o' && '(?:\\*\\.)?' || wild === 'r' && '(?:\\*\\.)' || '') + // start with *. if allowed/required `(?:[a-z\\d](?:[-a-z\\d]{0,61}[a-z\\d])?\\.)` + // [a-z0-9-] up to 63 chars, can't start or end w/ dash (subs === 'o' && '+' || subs === 'r' && '{2,}' || '') + // subdomains or not `(?!\\d+$)` + // tld can't be all digits `(?:[a-z\\d][-a-z\\d]{0,22}[a-z\\d])` + // tld up to 24 chars `))\\1$` // end capture , 'i').test(x); } function domain2(x, wild = 'n', subs = 'o') { if (x.indexOf('..') > 0 || x.indexOf('.') === -1 || x.indexOf('.-') > 0 || x.indexOf('-.') > 0 || x[0] === '-' || x[x.length - 1] === '-' ) { return false; } const parts = x.split('.'); // Wild bailout if (wild === 'r' && parts[0] !== '*') { return false; } else if (parts[0] === '*') { parts.shift(); } // Subdomain bailout if (subs === 'r' && parts.length < 3 || subs === 'n' && parts.length > 2) { return false; } const valid = /^[a-z0-9\-]+$/i; // tld btwn 2 and 24 chars, can't be all digits const tld = parts.pop(); if (!valid.test(tld) || /^\d+$/.test(tld) || tld.length > 24 || tld.length < 2) { return false; } return parts.every(p => { return p.length <= 63 && valid.test(p); }); }
Tests:
domain 1
domain1("*.google.com", 'o', 'n'); domain1("abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijk.com", 'n') domain1("abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvw.xyzabcdefghijk.com2.xyzabcdefghijk.com2.xyzabcdefghijk.com2.xyzabcdefghijk.com2.xyzabcdefghijk.com2.xyzabcdefghijk.com2.xyzabcdefghijk.com2.22", 'n') domain1("xn--d1ai6ai.xn--p1ai", 'r') domain1("*.x.XN--VERMGENSBERATUNG-PWB.co", 'o', 'r') domain1("*.x.XN--VERMGENSBERATUNG-PWB", 'o', 'n') domain1("mkyong.t.t.c", 'n') domain1("a.com")
domain 2
domain2("*.google.com", 'o', 'n'); domain2("abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijk.com", 'n') domain2("abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvw.xyzabcdefghijk.com2.xyzabcdefghijk.com2.xyzabcdefghijk.com2.xyzabcdefghijk.com2.xyzabcdefghijk.com2.xyzabcdefghijk.com2.xyzabcdefghijk.com2.22", 'n') domain2("xn--d1ai6ai.xn--p1ai", 'r') domain2("*.x.XN--VERMGENSBERATUNG-PWB.co", 'o', 'r') domain2("*.x.XN--VERMGENSBERATUNG-PWB", 'o', 'n') domain2("mkyong.t.t.c", 'n') domain2("a.com")
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
domain 1
domain 2
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'll explain the benchmark in detail. **Benchmark Overview** The provided JSON represents two JavaScript microbenchmarks: `domain1` and `domain2`, which test the performance of regular expression (regex) matching against string functions for validating domain names. **What is tested?** Both benchmarks test the execution speed of two approaches: * **Regex approach:** The first benchmark, `domain1`, uses a custom regex function to validate domain names. This function captures various aspects of a domain name, such as subdomains, top-level domains (TLDs), and tildes. * **String functions approach:** The second benchmark, `domain2`, uses string functions like `indexOf()`, `split()`, and regular expressions (`^` and `$`) to validate domain names. **Options compared** Both benchmarks compare the execution speed of two approaches: 1. Regex function in `domain1`. 2. String functions in `domain2`. **Pros and Cons:** * **Regex function (domain1)**: * Pros: * Can capture more complex domain name patterns, such as TLDs and tildes. * More flexible than string functions. * Cons: * Can be slower due to the complexity of the regex pattern. * May require more computational resources. * **String functions (domain2)**: * Pros: * Generally faster than regex functions. * Requires less computational resources. * Cons: * Less flexible than regex functions. **Library usage** Both benchmarks use a custom JavaScript function for the regex approach (`domain1`) and string functions (`domain2`). There is no library explicitly mentioned in the provided JSON data. However, we can infer that these custom functions are used to implement the validation logic for domain names. **Special JS features or syntax** Neither of the benchmarks uses any special JavaScript features or syntax beyond standard ECMAScript. They rely on basic JavaScript functionality like functions, strings, arrays, and regular expressions. **Other alternatives** For string function-based approaches like `domain2`, other alternatives could be: 1. **Regular expression libraries:** Utilizing existing regex libraries like `regex-js` or `simple-regexp` might provide performance benefits. 2. **TLD validation services:** Integrating with external TLD validation services can simplify the validation process and reduce computational overhead. For regex-based approaches like `domain1`, alternatives could include: 1. **Regex engines:** Using specialized regex engines like `regex-engines` or `fast-regex` might improve performance. 2. **Domain name APIs:** Leveraging domain name APIs, such as those provided by DNS service providers, can simplify the validation process and reduce computational overhead. In summary, both benchmarks compare the execution speed of two approaches: a regex function and string functions for validating domain names. The choice between these approaches depends on the specific requirements of the use case, including performance considerations and flexibility needs.
Related benchmarks:
CORS Test
regex vs string functions
regex vs string functions2
Regex tests Dani
Comments
Confirm delete:
Do you really want to delete benchmark?