whatwg-url vs url-parse vs url vs query-string vs url-search-params
URL and Query String Manipulation Comparison
1 Year
whatwg-urlurl-parseurlquery-stringurl-search-paramsSimilar Packages:
What's URL and Query String Manipulation?

URL and Query String Manipulation libraries in JavaScript provide tools for parsing, constructing, and manipulating URLs and their components, such as query strings, paths, and fragments. These libraries help developers handle URL-related tasks more efficiently, whether for client-side applications, server-side code, or APIs. They offer features like encoding/decoding query parameters, handling nested or array-like parameters, and ensuring proper URL formatting, which is essential for web development, routing, and data transmission. query-string is a lightweight library focused on parsing and stringifying query strings, while url is a built-in Node.js module that provides comprehensive URL handling. url-parse is a small, fast library for parsing URLs, and url-search-params is a polyfill for the URLSearchParams API, which provides a simple interface for working with query parameters. whatwg-url is a reference implementation of the URL Standard, offering a robust and compliant way to work with URLs in JavaScript.

Package Weekly Downloads Trend
Github Stars Ranking
Stat Detail
Package
Downloads
Stars
Size
Issues
Publish
License
whatwg-url97,313,28840098.6 kB55 months agoMIT
url-parse28,821,5401,03563 kB13-MIT
url21,519,77338278.3 kB17a year agoMIT
query-string14,279,6806,87053 kB28a month agoMIT
url-search-params60,215761-07 years agoMIT
Feature Comparison: whatwg-url vs url-parse vs url vs query-string vs url-search-params

Size and Performance

  • whatwg-url:

    whatwg-url is a reference implementation of the URL Standard, which is more comprehensive than other libraries. While it may be larger than some lightweight alternatives, it provides a thorough and compliant solution for handling URLs, making it suitable for applications that require strict adherence to web standards.

  • url-parse:

    url-parse is a lightweight library (around 1KB minified) designed for fast URL parsing. Its small size and quick parsing capabilities make it suitable for both browser and Node.js applications, especially when performance is a priority.

  • url:

    The url module is built into Node.js, so it has no additional size overhead for server-side applications. It provides comprehensive URL handling, but its performance is optimized for server environments rather than client-side use.

  • query-string:

    query-string is a small library (about 1KB minified) that offers efficient parsing and stringifying of query strings with minimal impact on performance. Its lightweight nature makes it ideal for client-side applications where load time and bandwidth are concerns.

  • url-search-params:

    url-search-params is a polyfill for the URLSearchParams API, which is lightweight and does not significantly impact performance. It is useful for adding standardized query parameter handling to environments that lack native support.

API Design

  • whatwg-url:

    whatwg-url provides a detailed and standards-compliant API for URL manipulation. It follows the URL Standard closely, offering methods and properties that align with modern web specifications. This makes it a reliable choice for developers who need a compliant and comprehensive URL handling solution.

  • url-parse:

    url-parse offers a straightforward API for parsing URLs. It breaks down the URL into its components (protocol, host, pathname, etc.) and allows easy access and manipulation of these parts. The API is simple and well-suited for quick URL parsing tasks.

  • url:

    The url module in Node.js offers a robust API for URL manipulation, including parsing, formatting, and resolving URLs. It provides detailed access to all URL components, but its API can be complex for beginners due to its comprehensive nature.

  • query-string:

    query-string provides a simple and intuitive API for parsing and stringifying query strings. It supports nested objects and arrays, making it easy to work with complex query parameters. The API is well-documented and easy to use, even for developers who are not familiar with query string manipulation.

  • url-search-params:

    url-search-params provides a clean and simple API for working with query parameters. It allows easy retrieval, modification, and deletion of parameters, and supports iteration over them. The API is designed to be intuitive and aligns with the standard URLSearchParams interface.

Query Parameter Handling

  • whatwg-url:

    whatwg-url provides comprehensive support for query parameters as part of its URL implementation. It adheres to the URL Standard, offering robust methods for manipulating query parameters, including support for nested and complex structures.

  • url-parse:

    url-parse handles query parameters as part of its URL parsing functionality. It provides access to the query string and allows for basic manipulation, but it does not offer advanced features for handling complex or nested query parameters.

  • url:

    The url module provides basic query parameter handling through its URL and URLSearchParams classes. While it supports standard query parameter manipulation, it is not as specialized or flexible as dedicated query string libraries.

  • query-string:

    query-string excels at handling complex query parameters, including nested objects and arrays. It provides options for customizing the parsing and stringifying processes, such as specifying delimiters and encoding. This makes it highly flexible for working with a wide variety of query string formats.

  • url-search-params:

    url-search-params focuses on query parameter manipulation, providing a standard interface for adding, removing, and modifying parameters. It supports iteration and works well with both simple and complex query strings, making it a reliable choice for most use cases.

Ease of Use: Code Examples

  • whatwg-url:

    whatwg-url provides a compliant way to work with URLs according to the URL Standard. Here’s a simple example:

    import { URL } from 'whatwg-url';
    
    const myUrl = new URL('https://example.com/path?name=John&age=30');
    
    // Accessing URL components
    console.log(myUrl.hostname); // Output: 'example.com'
    console.log(myUrl.pathname); // Output: '/path'
    console.log(myUrl.searchParams.get('name')); // Output: 'John'
    
    // Modifying URL components
    myUrl.pathname = '/new-path';
    myUrl.searchParams.append('city', 'New York');
    
    console.log(myUrl.toString()); // Output: 'https://example.com/new-path?name=John&age=30&city=New+York'
    
  • url-parse:

    url-parse provides a simple way to parse URLs and access their components. Here’s an example:

    import urlParse from 'url-parse';
    
    const parsedUrl = urlParse('https://example.com/path?name=John&age=30');
    
    console.log(parsedUrl.protocol); // Output: 'https:'
    console.log(parsedUrl.hostname); // Output: 'example.com'
    console.log(parsedUrl.query.name); // Output: 'John'
    
    // Modifying the URL
    parsedUrl.set('query', { name: 'Jane', city: 'New York' });
    console.log(parsedUrl.toString()); // Output: 'https://example.com/path?name=Jane&city=New+York'
    
  • url:

    The url module in Node.js provides powerful tools for URL manipulation, but it can be more complex to use due to its comprehensive nature. Here’s a basic example:

    const { URL } = require('url');
    
    // Creating a new URL
    const myUrl = new URL('https://example.com/path?name=John&age=30');
    
    // Accessing URL components
    console.log(myUrl.hostname); // Output: 'example.com'
    console.log(myUrl.pathname); // Output: '/path'
    console.log(myUrl.searchParams.get('name')); // Output: 'John'
    
    // Modifying URL components
    myUrl.pathname = '/new-path';
    myUrl.searchParams.append('city', 'New York');
    
    console.log(myUrl.toString()); // Output: 'https://example.com/new-path?name=John&age=30&city=New+York'
    
  • query-string:

    query-string makes it easy to work with query strings in a way that is both efficient and intuitive. Here’s a simple example of how to use it:

    import { parse, stringify } from 'query-string';
    
    // Parsing a query string
    const parsed = parse('?foo=bar&baz=qux&nested%5Bkey%5D=value');
    console.log(parsed);
    // Output: { foo: 'bar', baz: 'qux', nested: { key: 'value' } }
    
    // Stringifying an object into a query string
    const queryString = stringify({ foo: 'bar', baz: 'qux', nested: { key: 'value' } });
    console.log(queryString);
    // Output: 'foo=bar&baz=qux&nested%5Bkey%5D=value'
    
  • url-search-params:

    url-search-params provides a simple interface for working with query parameters. Here’s an example:

    const params = new URLSearchParams('?name=John&age=30&city=New+York');
    
    // Accessing parameters
    console.log(params.get('name')); // Output: 'John'
    console.log(params.get('age')); // Output: '30'
    
    // Modifying parameters
    params.set('age', '31');
    params.append('country', 'USA');
    params.delete('city');
    
    console.log(params.toString()); // Output: 'name=John&age=31&country=USA'
    
How to Choose: whatwg-url vs url-parse vs url vs query-string vs url-search-params
  • whatwg-url:

    Choose whatwg-url if you require a compliant implementation of the URL Standard for handling URLs in a way that adheres to modern web specifications.

  • url-parse:

    Choose url-parse if you want a small, fast library for parsing URLs that provides a simple API and supports both browser and Node.js environments.

  • url:

    Choose url if you need a comprehensive solution for URL manipulation in Node.js, including support for all URL components, parsing, and serialization.

  • query-string:

    Choose query-string if you need a lightweight, easy-to-use library specifically for parsing and stringifying query strings, especially if you work with complex or nested query parameters.

  • url-search-params:

    Choose url-search-params if you need a polyfill for the URLSearchParams API, which provides a standard way to work with query parameters in a more structured manner.

README for whatwg-url

whatwg-url

whatwg-url is a full implementation of the WHATWG URL Standard. It can be used standalone, but it also exposes a lot of the internal algorithms that are useful for integrating a URL parser into a project like jsdom.

Specification conformance

whatwg-url is currently up to date with the URL spec up to commit 6c78200.

For file: URLs, whose origin is left unspecified, whatwg-url chooses to use a new opaque origin (which serializes to "null").

whatwg-url does not yet implement any encoding handling beyond UTF-8. That is, the encoding override parameter does not exist in our API.

API

The URL and URLSearchParams classes

The main API is provided by the URL and URLSearchParams exports, which follows the spec's behavior in all ways (including e.g. USVString conversion). Most consumers of this library will want to use these.

Low-level URL Standard API

The following methods are exported for use by places like jsdom that need to implement things like HTMLHyperlinkElementUtils. They mostly operate on or return an "internal URL" or "URL record" type.

The stateOverride parameter is one of the following strings:

The URL record type has the following API:

These properties should be treated with care, as in general changing them will cause the URL record to be in an inconsistent state until the appropriate invocation of basicURLParse is used to fix it up. You can see examples of this in the URL Standard, where there are many step sequences like "4. Set context object’s url’s fragment to the empty string. 5. Basic URL parse input with context object’s url as url and fragment state as state override." In between those two steps, a URL record is in an unusable state.

The return value of "failure" in the spec is represented by null. That is, functions like parseURL and basicURLParse can return either a URL record or null.

whatwg-url/webidl2js-wrapper module

This module exports the URL and URLSearchParams interface wrappers API generated by webidl2js.

Development instructions

First, install Node.js. Then, fetch the dependencies of whatwg-url, by running from this directory:

npm install

To run tests:

npm test

To generate a coverage report:

npm run coverage

To build and run the live viewer:

npm run prepare
npm run build-live-viewer

Serve the contents of the live-viewer directory using any web server.

Supporting whatwg-url

The jsdom project (including whatwg-url) is a community-driven project maintained by a team of volunteers. You could support us by:

  • Getting professional support for whatwg-url as part of a Tidelift subscription. Tidelift helps making open source sustainable for us while giving teams assurances for maintenance, licensing, and security.
  • Contributing directly to the project.