SeanSobey / ChartjsNodeCanvas

A node renderer for Chart.js using canvas.
MIT License
230 stars 79 forks source link

Repeated require/freshRequire leak memory and prevent effective just-in-time compilation #145

Open smarr opened 7 months ago

smarr commented 7 months ago

This is more an attempt to document the issue (there's a change here that others might want to try though: https://github.com/smarr/ChartjsNodeCanvas/commit/66b8e9b8a0751b972916d4f6826ba7d48af32d7c).

I assume ChartjsNodeCanvas is designed to ensure perfect isolation of plugins, and supporting all kind of Chart.js plugins. To achieve this, I assume, the idea was to just require plugins and other code separately to ensure complete isolation.

However, this also leads to high memory overhead and additional costs such as requiring extra compilation by Node.js for every single canvas. So, creating a canvas is prohibitively expensive, perhaps more so that the documentation suggests already. The other problem is that requiring large chunks of code again and again leaks memory for the code, and then for the native compilation. Node.js doesn't really like this.

In my fork, I stripped out all require()/freshRequire() calls since I don't need the support for legacy plugins and plugin isolation. So, that's not something I can generally recommend. However, it comes at major performance gains of cutting chart render time by 50-80% in my case.