-
I thought I'd bring this up here before opening a ticket on this just in case I'm actually doing something wrong here. Recently I've been working on moving a project over to ESM. One thing I noticed was that loading the
Adds about ten seconds to the load time of my application. I have narrowed it down to the above import statement. If I import like so:
The app takes about 6 seconds to load. If I isolate this code to a test file:
The timing here tracks but for some reason it takes ten times as long for my app to start. I used the
Definitely looks like this is as a result of the ESM loader. The same thing happens when I use lodash, however, I can import specific functions like this:
Which almost completely eliminates the loading delay. I've tried this in various ways with mathjs but it doesn't look like it's possible? Side note: moving this project to ESM has been one of the most painful development experiences I've had in years. Can only imagine what a nightmare converting a larger project with multiple developers would be. My personal policy now is that I will NOT convert any more of my projects unless absolutely necessary. Only new projects will be using ESM. I'm glad that mathjs continues to support CJS because I use it in several places. |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments
-
I don't know how you collect 10 extra seconds, but ESM is definitely slower for me too, issue: nodejs/node#44186 test_math.cjs const { round } = require('mathjs'); test_math.mjs import { round } from 'mathjs'
Both still relatively fast, most time is spent in the file system, e.g. loading the browser bundle: const { round } = require('./node_modules/mathjs/lib/browser/math.js'); real 0m0,116s
user 0m0,100s
sys 0m0,021s Which node version, which OS? I'm on Linux Mint and |
Beta Was this translation helpful? Give feedback.
-
Node v18.17.1, Docker on Debian 11 x64, Proxmox VM on enterprise servers with AMD EPYC 7371 procs and NVMe drives. I wrote a small test app to more clearly illustrate the difference in load times: https://github.com/brenc/testmathjs On my dev VM I'm seeing lodash load about 15x faster than mathjs. Tested on my Macbook M1 Pro as well and am seeing the same thing. Here is a typical result on my dev VM:
And on my Macbook Pro:
Not sure why this leads to 10s+ load times in my app but I ended up just switching this one app to the lodash provided round function for now. |
Beta Was this translation helpful? Give feedback.
-
Just using mathjs for the I've indeed also noticed that ESM is much slower than CJS. We cannot do anything about that in this library though. mathjs is quite a large library, and by default it loads the whole library, which is much if you only use a single function. The ESM and CJS contains hundreds of files, since every function is written in a separate file. Loading separate files is relatively slow, and for example loading the UMD bundle of mathjs is much faster. We are making plans to make mathjs more modular, but that will take time. |
Beta Was this translation helpful? Give feedback.
Just using mathjs for the
round
function looks like overkill, if a more lightweight library like lodash works better just use that :).I've indeed also noticed that ESM is much slower than CJS. We cannot do anything about that in this library though.
mathjs is quite a large library, and by default it loads the whole library, which is much if you only use a single function. The ESM and CJS contains hundreds of files, since every function is written in a separate file. Loading separate files is relatively slow, and for example loading the UMD bundle of mathjs is much faster. We are making plans to make mathjs more modular, but that will take time.