Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Why instantiate a new markdown renderer for each file processed? #57

Closed
oravecz opened this issue Nov 21, 2018 · 3 comments · Fixed by #135
Closed

Why instantiate a new markdown renderer for each file processed? #57

oravecz opened this issue Nov 21, 2018 · 3 comments · Fixed by #135

Comments

@oravecz
Copy link

oravecz commented Nov 21, 2018

I assume the rendering process would execute much faster if the Markdown parser was configured once, then used repeatedly. Currently, for each file processed, the renderer is instantiated, all the plugins loaded, and configured, and finally, the text is processed.

Is there a reason for this? Perhaps the plugins or the markdown renderer is stateful?

@oravecz
Copy link
Author

oravecz commented Nov 21, 2018

I forked the code and did a quick hack to instantiate/configure the markdown parser only once. Seems to result in the same output for my limited tests.

I have 27 markdown files in my source folder.
Current code: 400ms
Optimized code: 320ms

Not as impressive as I expected the performance difference to be. I guess most of the time is spent in the initial require() commands to load/parse each plugin. Once those are cached, the rest of the overhead in configuring each plugin is minimal.

@curbengh
Copy link
Contributor

May I know what did you change to make this renderer only instantiate once?

@SukkaW
Copy link
Member

SukkaW commented Sep 26, 2020

May I know what did you change to make this renderer only instantiate once?

@curbengh As you can see, we require markdown-it whenever the renderer function is called:

const MdIt = require('markdown-it');

We also new a markdown-it instance whenever the renderer function is called:

let parser = new MdIt(preset, render);

We could lift them to the top.

yoshinorin added a commit to yoshinorin/hexo-renderer-markdown-it that referenced this issue Oct 17, 2020
yoshinorin added a commit to yoshinorin/hexo-renderer-markdown-it that referenced this issue Jan 1, 2022
yoshinorin added a commit that referenced this issue Jan 2, 2022
)

* pref(#57): avoid creating instance each time when render each file

* fix: lint
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants