Regex VS Constructor (v2)

Revision 2 of this benchmark created on


Setup

// The URL we're testing
const urlToTest = "http://valid-url.com";

Test runner

Ready to run.

Testing in
TestOps/sec
Regex
// Regex to check if it's a URL
const regex = new RegExp('^(http|https)://[a-zA-Z0-9-.]+.[a-zA-Z]{2,3}(/[^\s]*)?$');

// Function to check URL using Regex
const checkURLRegex = (url) => regex.test(url);

checkURLRegex(urlToTest);
ready
Constructor
// Function to check URL using the URL constructor
const checkURLConstructor = (url) => {
    try {
        new URL(url);
        return true;
    } catch {
        return false;
    }
};

checkURLConstructor(urlToTest);
ready

Revisions

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