helpers: getUrls() is quadratic in the length of one word

Open
#906 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
52/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
javascript, node.js

Research direction

Start by reading getUrls() in @metascraper/helpers and the URL regex, then inspect test/equivalence.js for the compatibility corpus. Compare how candidate start positions are handled and run the reproducer with increasingly long words. Done means a 100k-character word completes in a few milliseconds while returning the same matches as the equivalence corpus.

Written by the indexing model from the issue text.

Description

What happens

After #904's fix, getUrls() in @metascraper/helpers is no longer exponential, but it is still quadratic in the length of one whitespace-free word. The URL regex is unanchored in match mode. At every start position inside a long run of host characters, the attempt reads to the end of the run before it fails.

Reproduce

const { getUrls } = require('@metascraper/helpers')
for (const n of [10000, 20000, 40000]) {
  const t = performance.now()
  getUrls('a'.repeat(n))
  console.log(n, (performance.now() - t).toFixed(0) + 'ms')
}

With #904 applied (Node 26, M-series laptop): 10k 273 ms, 20k 1,101 ms, 40k 4,461 ms. On master a 5k word already takes 25 s.

Expected

Linear time with the same matches. re2 is linear, but it is not a drop-in replacement. It reads \s as ASCII only, [¡-￿] excludes astral characters, and its case folding maps ſ to s. So the fix has to keep JavaScript semantics, for example a scanner that skips start positions where no match can begin.

Who gets it

metascraper-x calls it on X post descriptions, which can be up to 25k characters.

Done when

getUrls() on a 100k-character word finishes in a few milliseconds and returns the same matches as today on the equivalence corpus in test/equivalence.js.

🤖 Generated with Claude Code

Dominant language
HTML
Stars
2.8k
Forks
183
Avg merge
1h 16m
Merged PRs (30d)
9

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from microlinkhq/metascraper

All issues in microlinkhq/metascraper

Similar issues

More Backend & API Design issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.