Skip to content

perf: decode named entities with character-entities - #40

Merged
serkodev merged 3 commits into
serkodev:mainfrom
onmax:feat/decode-named-character-reference
Nov 3, 2026
Merged

serkodev merged 3 commits into
serkodev:mainfrom
onmax:feat/decode-named-character-reference

Conversation

@onmax

@onmax onmax commented Nov 2, 2026 •

Copy link
Copy Markdown
Contributor

resolves #38

because entities' decodeHTML ships the HTML5 decode table as a packed trie, markdown-exit's bundle carries 31.5 KiB of table that gzip barely shrinks. character-entities, the table micromark and remark use, holds the same names as a plain object.

this looks named references up in it directly, with no DOM, so it behaves the same in Node, browsers, and workers. unescapeAll used non-strict decodeHTML, so in link destinations and titles an unknown name like &ampfoo; decoded its &amp prefix and became &foo;, and overlong numeric references like A decoded too. both now stay literal, as CommonMark requires, with a test.

bundle of src/index.ts KiB minified / gzip
main (entities@7) 108.2 / 46.1
this PR 104.9 / 35.9

esbuild 0.28, minified ESM, gzip -9. against entities@9 from #37 the table is 13.5 vs 11.7 KiB gzip.

@onmax onmax changed the title perf: decode named entities with decode-named-character-reference perf: decode named entities with character-entities Nov 2, 2026
onmax and others added 3 commits November 3, 2026 15:54
entities' decodeHTML ships a packed decode trie that gzip barely
shrinks. decode-named-character-reference ships the same names as an
object, and decodes through the DOM in browsers.

It also only decodes complete names, so link destinations and titles
keep unknown names like `&ampfoo;` literal, as CommonMark requires.
decode-named-character-reference's browser export decodes through
innerHTML, which fails in workers and under Trusted Types. A direct
character-entities lookup behaves the same everywhere.
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.

Use decode-named-character-reference for HTML entities?

2 participants