astro-community / md

Render any Markdown content in Astro, optionally integrating with any existing configuration.
https://stackblitz.com/github/astro-community/md
68 stars 7 forks source link

Abysmally slow build - not caching createMarkdownProcessor #16

Closed oliwarner closed 2 months ago

oliwarner commented 3 months ago

Each time I use <Markdown of={string} /> the page build appears to be extended by about 200-400ms (from 5ms). In practice this means some pages take multiple seconds to build. I'm not going to be able to deploy like this. I've disabled all custom markdown config, stripped everything right back, but it's still adding a massive overhead.

Looking at the md source, every <Markdown.../> tag, every markdown() call builds its own processor via createMarkdownProcessor(). It's passing in fresh options each time. I haven't profiled it, but this seems like a really expensive operation given the options are always the same, right?

My reading of the main Astro markdown build process is that one instance of the processor gets used for multiple files. I'm not certain how Vite does its magic so I could be wrong.

Either which way, it seems likely this library should be caching and reusing a single processor, or one per set of options if there are multiple options being used.

oliwarner commented 3 months ago

Yeah, moving to a single createMarkdownProcessor call speeds things up tremendously. The first page takes a 200-400ms hit, the remaining all complete at full speed. I've submitted a PR.

Now, I'm not sure if this has repercussions, or whether a top-level variable is the best way to do this in the modern age, but… WFM.

BadMannersXYZ commented 2 months ago

As a palliative fix, I have markdown: { syntaxHighlight: false } in my Astro config to avoid most of the performance hit from this. I can't wait for the next release of this library to get rid of my hacky solution!