helpers: title() and description() are quadratic through smartquotes

Open
#908 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
65/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
javascript, node.js

Research direction

Read the title() and description() paths in @metascraper/helpers and inspect where titleize calls the third-party smartquotes rule. Compare outputs against smartquotes on a corpus, following test/equivalence.js; then add a timing test in packages/metascraper-helpers/test/linear-time.js. Done means both functions handle 100k-character quote-heavy input in a few milliseconds with the same output.

Written by the indexing model from the issue text.

Description

What happens

title() and description() in @metascraper/helpers (5.58.11, master 5b0dbfeb; unchanged by #907) take time quadratic in the length of their input when it alternates quotes and letters. Both call titleize, which runs smartquotes. smartquotes has a rule (\B|^)‘(?=([^‘’]*’\b)*…), and its lookahead rescans the rest of the string from every candidate quote.

This is a different cause from #905 (REGEX_LOCATION on a whitespace run).

Reproduce

const { title, description } = require('@metascraper/helpers')
const time = (name, fn) => { const t = performance.now(); fn(); console.log(name, (performance.now() - t).toFixed(0) + 'ms') }
for (const n of [25000, 50000, 100000]) {
  time(`title "a' ${n}`, () => title('"a\''.repeat(n / 3)))
  time(`description "a' ${n}`, () => description('"a\''.repeat(n / 3)))
  time(`title a 'b ${n}`, () => title("a 'b".repeat(n / 4)))
}

Measured locally (Node 26, M-series laptop):

input 25k 50k 100k
title('"a\''.repeat(n / 3)) 83 ms 327 ms 1,330 ms
description('"a\''.repeat(n / 3)) 84 ms 335 ms 1,328 ms
title("a 'b".repeat(n / 4)) 62 ms 243 ms 1,016 ms

On master, title('"a\''.repeat(33334)) takes 1,351 ms. A separate review measured over 30 s at 1M characters.

Expected

Linear time and the same output. smartquotes is third-party, so either replace the rule with a linear pass in helpers (and check equivalence against smartquotes on a corpus, as test/equivalence.js does for the URL regex), or fix it upstream.

Who gets it

Anyone who runs metascraper on HTML they do not control: every title and description rule passes the page's value through titleize.

Done when

title() and description() on a 100k-character quote-heavy input each finish in a few milliseconds, with a timing test in packages/metascraper-helpers/test/linear-time.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.