Check anchor path

Benchmark created on


Description

Which way if faster to check for a given path in an anchor tag href attribute

Preparation HTML

<a id="lnk1" href="https://www.google.com/search?q=puppies&rlz=1CAVLER_enUS922&oq=puppi&gs_lcrp=EgZjaHJvbWUqCQgAECMYJxiKBTIJCAAQIxgnGIoFMgsIARBFGCcYORiKBTIGCAIQRRg8MgYIAxBFGDwyBggEEEUYPDIGCAUQRRg8MgYIBhBFGDwyBggHEEUYPNIBCDEwNjNqMGo3qAIAsAIA&sourceid=chrome&ie=UTF-8">Google Puppies</a>
<a id="lnk2" href="https://mmm.google.com/finder?q=puppies&rlz=1CAVLER_enUS922&oq=puppi&gs_lcrp=EgZjaHJvbWUqCQgAECMYJxiKBTIJCAAQIxgnGIoFMgsIARBFGCcYORiKBTIGCAIQRRg8MgYIAxBFGDwyBggEEEUYPDIGCAUQRRg8MgYIBhBFGDwyBggHEEUYPNIBCDEwNjNqMGo3qAIAsAIA&sourceid=chrome&ie=UTF-8">Google Puppies</a>

Setup

const lnk1 = document.getElementById('lnk1');
const lnk2 = document.getElementById('lnk2');
const reLnk = /^https:\/\/www\.google\.com\/search\?.*/;

Test runner

Ready to run.

Testing in
TestOps/sec
IndexOf
return lnk1.href.indexOf('https://www.google.com/search?') > -1 && lnk2.href.indexOf('https://www.google.com/search?') === -1;
ready
RegExp
return reLnk.test(lnk1.href) && !reLnk.test(lnk2.href);
ready
URL
const u1 = new URL(lnk1.href);
const u2 = new URL(lnk2.href);
return u1.origin == 'https://www.google.com' && u1.pathname == '/search' && !(u2.origin == 'https://www.google.com' && u2.pathname == '/search');
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.