From b73582944b23392e1c8bb88b9e9740387fdc62ab Mon Sep 17 00:00:00 2001 From: onmax Date: Mon, 16 Nov 2026 17:26:28 +0100 Subject: [PATCH] fix(tinylinkify): leave www out of the URL search when it doesn't link www. With fuzzyLink off, every www match was turned down, and each one read the rest of its host again: 80 KB took 3.7 seconds. The search now has no www to find then. The fix for rejected candidates in #35 grew the root and tinylinkify/gfm past their size budgets, by about 120 B gzip, so the budgets go to 1985 B and 2460 B and the sizes in the docs to match. --- .github/drop-ins.json | 2 +- README.md | 2 +- docs/pages/tinylinkify/size-and-speed.md | 4 ++-- packages/tinylinkify/README.md | 10 +++++----- packages/tinylinkify/scripts/size.ts | 4 ++-- packages/tinylinkify/src/core/gfm.ts | 8 +++++--- packages/tinylinkify/test/gfm.test.ts | 9 +++++++++ 7 files changed, 25 insertions(+), 14 deletions(-) diff --git a/.github/drop-ins.json b/.github/drop-ins.json index 6ab491e..3f064c9 100644 --- a/.github/drop-ins.json +++ b/.github/drop-ins.json @@ -356,7 +356,7 @@ "entry": "tinylinkify", "gzip": { "theirs": 4211, - "ours": 1836 + "ours": 1955 }, "parse": { "theirs": 5.666320437499962, diff --git a/README.md b/README.md index 392e2c9..5bd7a45 100644 --- a/README.md +++ b/README.md @@ -24,7 +24,7 @@ Smaller, faster, dependency-free parsers and linkifiers you can drop in for the | [tinyaml](./packages/tinyaml) | YAML | [7.7 KiB](./packages/tinyaml#size-and-speed) | js-yaml 4 and 5, yaml 2, yamljs, @std/yaml | | [tinyjson5](./packages/tinyjson5) | JSON5 | [3.4 KiB](./packages/tinyjson5#size) | json5 2.2.3 | | [tinyini](./packages/tinyini) | INI | [1.3 KiB](./packages/tinyini#size) | ini 1.3.8, 2 to 4.0 and 4.1.3 to 7, @one-ini/wasm 0.1.1 to 0.2 | -| [tinylinkify](./packages/tinylinkify) | Links in text | [1.8 KiB](./packages/tinylinkify#size) | linkify-it 4 and 5 | +| [tinylinkify](./packages/tinylinkify) | Links in text | [1.9 KiB](./packages/tinylinkify#size) | linkify-it 4 and 5 | Bundle sizes include every export of each package's root API, minified and gzipped. See [Size and speed](#size-and-speed) for the drop-in replacements. diff --git a/docs/pages/tinylinkify/size-and-speed.md b/docs/pages/tinylinkify/size-and-speed.md index 2ef7fa7..aa9828c 100644 --- a/docs/pages/tinylinkify/size-and-speed.md +++ b/docs/pages/tinylinkify/size-and-speed.md @@ -7,8 +7,8 @@ Each entry as npm ships it in `dist/`, bundled with esbuild, minified ESM, then | Entry | Minified | Gzip | | ------------------------ | --------- | -------- | -| `tinylinkify` | 3.55 KiB | 1.79 KiB | -| `tinylinkify/gfm` | 4.65 KiB | 2.22 KiB | +| `tinylinkify` | 3.87 KiB | 1.91 KiB | +| `tinylinkify/gfm` | 5.04 KiB | 2.37 KiB | | `tinylinkify/linkify-it` | 10.32 KiB | 3.90 KiB | | linkify-it 5.0.2 | 11.25 KiB | 4.11 KiB | | linkify-it 6.1.0 | 13.30 KiB | 4.40 KiB | diff --git a/packages/tinylinkify/README.md b/packages/tinylinkify/README.md index 66c299f..af01c9d 100644 --- a/packages/tinylinkify/README.md +++ b/packages/tinylinkify/README.md @@ -5,14 +5,14 @@ [![release](https://comark-ui.onmax.me/badge.svg?source=npm-version&package=tinylinkify&label=release&icon=npm)](https://github.com/onmax/tiny/releases) -[![size](https://comark-ui.onmax.me/badge.svg?label=size&value=1.8+KiB+gzip)](#size) +[![size](https://comark-ui.onmax.me/badge.svg?label=size&value=1.9+KiB+gzip)](#size) [![dependencies](https://comark-ui.onmax.me/badge.svg?label=dependencies&value=0)](https://www.npmjs.com/package/tinylinkify?activeTab=dependencies) [![license: MIT](https://comark-ui.onmax.me/badge.svg?label=license&value=MIT)](./LICENSE) [![Release status](https://comark-ui.onmax.me/badge.svg?source=github-actions&repository=onmax%2Ftiny&workflow=release.yaml&icon=github)](https://github.com/onmax/tiny/actions/workflows/release.yaml) -**Links in plain text, in 1.8 KiB.** No dependencies, GFM's autolink rules as remark-gfm and GitHub apply them, and up to 3 times faster than linkify-it. +**Links in plain text, in 1.9 KiB.** No dependencies, GFM's autolink rules as remark-gfm and GitHub apply them, and up to 3 times faster than linkify-it. Already on linkify-it? Change one import and keep your code: @@ -29,7 +29,7 @@ tinylinkify ships a drop-in replacement for linkify-it 5 (and 4), and `tinylinki | ---------------- | ------------------------ | ------------: | ----------: | ----------: | | linkify-it 5.0.2 | `tinylinkify/linkify-it` | 4.1 → 3.9 KiB | 1.1× faster | — | -The root `tinylinkify` API, which finds GFM links, is 1.8 KiB against 4.1 KiB for linkify-it 5.0.2, and finds them 2.8× faster than linkify-it finds its own in the same text. +The root `tinylinkify` API, which finds GFM links, is 1.9 KiB against 4.1 KiB for linkify-it 5.0.2, and finds them 2.8× faster than linkify-it finds its own in the same text. Size is the minified bundle, gzipped. Speed is the median time to parse and stringify the large benchmark file. linkify-it only finds links, so its time is `match` on 100 KB of text. Measured on AMD Ryzen 7 255 w/ Radeon 780M Graphics, Node 24.21.0. @@ -138,8 +138,8 @@ Each entry as npm ships it in `dist/`, bundled with esbuild, minified ESM, then | Entry | Minified | Gzip | | ------------------------ | --------- | -------- | -| `tinylinkify` | 3.55 KiB | 1.79 KiB | -| `tinylinkify/gfm` | 4.65 KiB | 2.22 KiB | +| `tinylinkify` | 3.87 KiB | 1.91 KiB | +| `tinylinkify/gfm` | 5.04 KiB | 2.37 KiB | | `tinylinkify/linkify-it` | 10.32 KiB | 3.90 KiB | | linkify-it 5.0.2 | 11.25 KiB | 4.11 KiB | | linkify-it 6.1.0 | 13.30 KiB | 4.40 KiB | diff --git a/packages/tinylinkify/scripts/size.ts b/packages/tinylinkify/scripts/size.ts index ecea59e..aa32878 100644 --- a/packages/tinylinkify/scripts/size.ts +++ b/packages/tinylinkify/scripts/size.ts @@ -28,8 +28,8 @@ const entries: [string, string, string?][] = [ // Gzip bytes each entry may reach, about 30 over its size when last set. // Raise one on purpose, and say why in the commit. const BUDGET: Record = { - tinylinkify: 1870, - "tinylinkify/gfm": 2300, + tinylinkify: 1985, + "tinylinkify/gfm": 2460, "tinylinkify/linkify-it": 4030, }; diff --git a/packages/tinylinkify/src/core/gfm.ts b/packages/tinylinkify/src/core/gfm.ts index 9c85c8f..f00a61f 100644 --- a/packages/tinylinkify/src/core/gfm.ts +++ b/packages/tinylinkify/src/core/gfm.ts @@ -131,6 +131,9 @@ function protocol(i: number): [string, number] | undefined { // mdast-util-gfm-autolink-literal's searches, in its order. const URL = /(? a.index - b.index); const gaps: number[] = []; @@ -245,7 +248,7 @@ export function find(value: string, www = true, email = true, first = false): Li const [whole, a = "", b = "", rest] = m; let retry = m.index + 1; let ok = !/[\d\-_]$/.test(b); - if (search === URL) { + if (!s) { const c = run.charCodeAt(m.index - 1); let host = b; let scheme = a; @@ -258,7 +261,6 @@ export function find(value: string, www = true, email = true, first = false): Li const labels = host.split("."); ok = (!m.index || whitespace(c) || punctuation(c)) && - (www || !prefix) && !punctuation(host.charCodeAt(0)) && labels.length > 1 && !badLabel(labels.at(-1)) && diff --git a/packages/tinylinkify/test/gfm.test.ts b/packages/tinylinkify/test/gfm.test.ts index fc1375b..d6d41e9 100644 --- a/packages/tinylinkify/test/gfm.test.ts +++ b/packages/tinylinkify/test/gfm.test.ts @@ -136,6 +136,15 @@ describe("tinylinkify/gfm", () => { expect(performance.now() - start).toBeLessThan(1000); }); + it("doesn't read the rest of a domain at each www with fuzzyLink off", () => { + const text = `:// ${"www._".repeat(16_000)}.a.b https://example.com`; + const start = performance.now(); + expect(new LinkifyIt({ fuzzyLink: false }).match(text)?.at(-1)?.url).toBe( + "https://example.com", + ); + expect(performance.now() - start).toBeLessThan(500); + }); + it("is a plugin that turns linkify on, with its options", () => { const md = createMarkdownExit().use(linkifyGfm, { fuzzyEmail: false }); expect(md.render("see www.example.com, mail a.b@example.com")).toBe(