r/javascript • u/medikoo • Jul 16 '13
Browser load time comparison of CJS (Node.js) and AMD module systems. AMD turns out as x10 times slower.
https://github.com/medikoo/cjs-vs-amd-benchmark#cjsamd-load-time-benchmark4
u/evilagentcooper Jul 16 '13
Test doesn't seem to be fair. Require.js resolves dependencies in real time, while CJS is doing preprocessing on the server. Maybe you should try comparing CJS to Almond? And as far as I know there is no simple way to load module on-demand in CJS.
2
u/gatorcoder Jul 16 '13
This is not the same comparison at all. You let the server pre-bundle your deps with CJS. With require you let the browser determine your deps and then load them individually. We all know network requests are the bottleneck in this case. This is a case when you would have been using the require js optimizer anyway. I'd like to see a side by side comparison then, my guess is they will be roughly the same.
2
u/medikoo Jul 16 '13
There's no pre-bundle, script is bundled on each request. What is cached is just resolved dependencies and source of each file (module), if file was not modified it's reused, that's it.
RequireJS optimizer if I understand correctly prepares script for production. Here we compare speed of load in development mode. Comparing pre-generated bundles doesn't make much sense, as both AMD and CJS can be optimized and minified in many different ways, and difference between both should be negligible.
1
u/gatorcoder Jul 16 '13
I certainly understand your point and yes you are correct about requirejs and the optimizer, however I don't want all my JS combined while I am debugging locally. It is easier for me to find code in module X when it is in file modulex.js instead of one massive CJS file that is pre-combined on the server.
1
u/medikoo Jul 16 '13
Where did you get the notion that you need to work with a bundle?
You edit modules files naturally, and bundle is generated out of them, you must not edit the bundle.
1
u/kenman Jul 16 '13 edited Jul 16 '13
Don't forget that the RequireJS optimizer supports sourcemaps.
1
u/medikoo Jul 16 '13 edited Jul 16 '13
CJS resolution is also done in real time (on request). You can add/remove new dependencies to modules, and after page reload you'll see it loaded.
Still, to have fair numbers you need to install this benchmark and run on localhost. I have about 1s load time with AMD then.
1
u/evilagentcooper Jul 16 '13
You are right, but there is no overhead in loading tons of scripts. How would you handle a situation using CJS, when you need the module only when a button click occurs?
1
u/medikoo Jul 16 '13
evilagentcooper CJS overhead is minimal (about 60 lines of code when not minified), with AMD you need to take whole RequireJS on board which is much bigger.
1
u/evilagentcooper Jul 16 '13
I was talking about overhead in AMD when using it without compressing the modules (see Almond). It is obvious that loading 270 small scripts into the browser will take longer than loading one big script.
1
u/medikoo Jul 16 '13
Almond is for preparing script for production. Same with CJS you just prepare the static bundle, minify it, and do not rebuild in on request. Not sure what overhead you're talking about.
0
Jul 16 '13
[deleted]
2
u/medikoo Jul 16 '13
To see results, enter listed links.
Still it's just showcase (how it works when it's installed), fair results can only be obtained on localhost (AMD resolution is not affected then by network latency).
2
u/rhysbrettbowen Jul 16 '13
try running the requirejs system through r.js first