Skip to content

perf: optimize reference link masking - #4048

Open
sarthakagrawal927 wants to merge 2 commits into
markedjs:masterfrom
sarthakagrawal927:agent/optimize-reference-link-masking
Open

perf: optimize reference link masking#4048
sarthakagrawal927 wants to merge 2 commits into
markedjs:masterfrom
sarthakagrawal927:agent/optimize-reference-link-masking

Conversation

@sarthakagrawal927

@sarthakagrawal927 sarthakagrawal927 commented Aug 9, 2026

Copy link
Copy Markdown

Marked version: 681373c

Markdown flavor: n/a

Description

Reference-link masking rebuilt Object.keys(this.tokens.links) for every
queued inline source, then linearly searched it for every candidate. At 2,000
definitions and paragraphs, that is about four million temporary key entries
and two million string comparisons.

This checks the existing table with Object.hasOwn, removing both costs without
adding lexer state or changing output.

Performance

Thirty alternating fresh-process pairs on Node.js 26.7.0, with per-process
warmup. Values are medians; intervals are paired 95% bootstrap intervals.

References Baseline Current Change (95% interval)
100 0.805 ms/op 0.489 ms/op -39.2% (-40.3, -38.8)
500 11.512 ms/op 2.265 ms/op -80.4% (-80.5, -80.2)
2,000 164.115 ms/op 10.095 ms/op -93.8% (-93.9, -93.8)

A 2,000-paragraph no-definition control moved +1.3% (+0.3, +2.3). Guards that
avoid that scan also re-enumerated the link table and erased most of the target
gain, so this keeps the smaller implementation and scopes the claim.

Validation

  • npm test
  • 1,779 specification tests and 190 unit tests passed
  • ESM/UMD/CJS, type, build, and lint checks passed

Contributor

  • Existing reference-link unit and CommonMark/GFM specification tests cover
    the unchanged parsing behavior.
  • No documentation is required because this does not add or change a
    feature.

Committer

  • CI is green (no forced merge required).
  • Squash and Merge PR following conventional commit guidelines.

@vercel

vercel Bot commented Aug 9, 2026

Copy link
Copy Markdown

@sarthakagrawal927 is attempting to deploy a commit to the MarkedJS Team on Vercel.

A member of the Team first needs to authorize it.

@sarthakagrawal927
sarthakagrawal927 marked this pull request as ready for review August 9, 2026 05:23
@vercel

vercel Bot commented Aug 9, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
marked-website Ready Ready Preview Aug 10, 2026 3:25pm

Request Review

Comment thread src/Lexer.ts Outdated
Comment thread src/Lexer.ts Outdated
Comment thread src/Lexer.ts
let maskedSrc = src;

// Mask out reflinks
if (this.tokens.links) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should still check if (this.tokens.links && Object.entries(this.tokens.links).length > 0) to prevent checking if we don't need to

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants