npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

swift-url-check

v1.0.0

Published

Fast and robust URL validator

Downloads

101

Readme

swift-url-check

Fast and robust URL validator

Why?

This library provides a faster and more robust URL validation compared to is-url-superb. It uses optimized validation logic with early returns and efficient regex patterns to achieve better performance.

Features

  • Fast - Optimized for performance with early validation checks
  • 🛡️ Robust - Comprehensive validation including hostname, protocol, and structure checks
  • 🎯 Flexible - Configurable options for different use cases
  • 📦 Small - Zero dependencies
  • 🔧 TypeScript - Full TypeScript support with type definitions
  • 🌐 ESM & CommonJS - Supports both module systems

Install

npm install swift-url-check

Usage

import isUrl from 'swift-url-check';

isUrl('https://opensly.in');
//=> true

isUrl('not a url');
//=> false

isUrl('example.com');
//=> false

isUrl('example.com', { lenient: true });
//=> true

CommonJS

This package is ESM-only. For CommonJS support, use dynamic import:

const isUrl = (await import('swift-url-check')).default;

isUrl('https://example.com');
//=> true

TypeScript

import isUrl, { IsUrlOptions } from 'swift-url-check';

const options: IsUrlOptions = {
  lenient: true,
  allowLocal: false
};

isUrl('https://example.com', options);
//=> true

API

isUrl(string, options?)

Returns true if the string is a valid URL, false otherwise.

string

Type: string

The string to validate as a URL.

options

Type: object

lenient

Type: boolean
Default: false

Allow URLs without protocol. When true, strings like "example.com" will be considered valid.

isUrl('example.com');
//=> false

isUrl('example.com', { lenient: true });
//=> true
allowLocal

Type: boolean
Default: true

Allow localhost and local IP addresses. When false, localhost, 127.0.0.1, 192.168.x.x, 10.x.x.x, etc. will be rejected.

isUrl('http://localhost:3000');
//=> true

isUrl('http://localhost:3000', { allowLocal: false });
//=> false
protocols

Type: string[]
Default: ['http:', 'https:']

Specify which protocols are considered valid.

isUrl('ftp://example.com');
//=> false

isUrl('ftp://example.com', { protocols: ['ftp:', 'ftps:'] });
//=> true

Validation Rules

The validator checks for:

  • Valid protocol format
  • Proper hostname structure
  • No whitespace in URLs
  • Valid domain parts (labels)
  • Proper TLD format
  • IPv4 and IPv6 support
  • Port number validation
  • Path, query, and hash support

Testing

Run tests:

npm test

Run tests with coverage:

npm run test:coverage

Watch mode for development:

npm run test:watch

The library has comprehensive test coverage with 47 test cases covering:

  • Basic URL validation
  • URL components (paths, query strings, hash fragments, ports)
  • Subdomains and IP addresses
  • All configuration options
  • Edge cases and error conditions

Performance

Run the benchmark:

npm run benchmark

Compare with is-url-superb:

npm run benchmark:compare

Benchmark Results

1.89x faster than is-url-superb (88.5% improvement)

swift-url-check:  3,234,110 ops/sec (0.309µs per operation)
is-url-superb:    1,715,301 ops/sec (0.583µs per operation)

This library is optimized for speed with:

  • Early type checking
  • Fast-path validations before URL parsing
  • Efficient regex patterns
  • Minimal allocations

Comparison with is-url-superb

| Feature | swift-url-check | is-url-superb | |---------|-----------------|---------------| | Performance | ⚡ 1.89x Faster | Standard | | TypeScript | ✅ Built-in | ❌ Requires @types | | ESM Support | ✅ ESM-only | ✅ ESM-only | | Local URL Control | ✅ Configurable | ❌ No option | | Protocol Control | ✅ Configurable | ❌ Fixed | | Dependencies | 0 | 1 (is-url) |

License

MIT