@ghostery/url-parser

中文文档
1.3.1发布于 2026/01/09MPL-2.0TypeScriptESM + CJS构建来源已验证近 30 天下载 116.4万

A Fast URL parser implementation

原始使用文档
文档目录4 节
  1. Install
  2. Usage
  3. Performance
  4. License

Ghostery Url Parser

A Fast implementation of url parsing, mostly API-compatible with the standard URL class while being on average 2-3 times faster. Evaluation of URL components is lazy, so this implementation should be fast for all read use-cases.

Known differences to standard URL:

  • Parameters returned via URL.searchParams.entries() are decoded only with decodeURIComponent. This differs to standards parsing in some subtle ways.
  • You can iterate a URL parameters array directly via URL.searchParams.params. This is around 20% faster than using an iterator.
  • Parameter strings (; sepearated key/value pairs) are parsed, and accessible via URL.parameters.
  • Domain parsing with tldts is built in. The URL.domainInfo attribute returns output from tldts' parseHost method.
  • Hostname validation is not done on initial parse. The isValidHost() method is provided for this purpose.
  • All URLs with a valid authority are given an origin, regardless of the protocol scheme. This differs from the standard that only does so for a set of known schemes.

Install

npm install @ghostery/url-parser

Usage

const parsed = new URL('https://www.example.com');
parsed.hostname // == 'www.example.com'

Performance

We benchmark against a list of 250,000 URLs collected from popular sites, as previously used in our adblocker benchmark. We compare two use-cases:

  1. URL object creation: new URL(url)
  2. Query string parsing: new URL(url).searchParams.entries()

We compare to a reference implementation on each platform:

  • Node: URL class from the url library
  • Firefox: window.URL
  • Chrome: window.URL
  • Safari: window.URL
EnvironmentUse caseReference: urls/sGhostery parser: urls/sSpeedup
Node 11new URL()149,5141,577,71110.5x faster
Node 11searchParams140,544198,3401.4x faster
Firefox 69new URL()268,0661,043,8773.9x faster
Firefox 69searchParams119,207354,7933.0x faster
Chrome 75new URL()366,2941,721,9034.7x faster
Chrome 75searchParams144,309283,9562.0x faster
Safari 12new URL()656,9301,525,0782.3x faster
Safari 12searchParams264,481437,7381.7x faster

All benchmarks were run on a Mid 2015 Macbook Pro, 2.5 GHz Intel Core i7, 16GB.

License

Mozilla Public License 2.0

版本与发布记录

@ghostery/url-parser 共发布 4 个版本,首次发布于 2024/10/17;过去 12 个月发布了 1 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
1.3.1latest2026/01/0910 个月前
1.3.02025/06/121.4 年前
1.2.02025/05/291.5 年前
1.1.62024/10/172 年前

关于 @ghostery/url-parser 的常见问题

@ghostery/url-parser 是什么?

A Fast URL parser implementation

如何安装 @ghostery/url-parser?

在项目目录运行 npm install @ghostery/url-parser,或使用 pnpm add / yarn add / bun add。

@ghostery/url-parser 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@ghostery/url-parser 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@ghostery/url-parser 可以商用吗?

@ghostery/url-parser 使用 MPL-2.0 许可。弱 Copyleft:可以商用,也可以和闭源代码一起使用;但如果分发时修改了 MPL 授权的文件,这些文件的修改需以 MPL 开源。以上为概括,不构成法律意见,请以许可原文为准。

@ghostery/url-parser 的下载量有多少?

根据 npm 官方下载统计,@ghostery/url-parser 在 2026/10/17 – 2026/11/15 这 30 天内被下载 1,163,920 次,日均约 38,797 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@ghostery/url-parser 还在维护吗?

最新版本 1.3.1 发布于 2026/01/09(10 个月前);至今共发布 4 个版本,其中过去 12 个月发布了 1 个;首次发布于 2024/10/17。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。