r/javascript • • 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-benchmark
1 Upvotes

22 comments sorted by

2

u/rhysbrettbowen Jul 16 '13

try running the requirejs system through r.js first

1

u/medikoo Jul 16 '13

I don't see any point of development with AMD when you still generate bundle on a server. CJS with NPM are much more powerful for that, and still surely faster.

Point of this benchmark was to show, that asynchronous resolution in a browser is slower, than on request (server-side) bundle generation. Many devs assume it's opposite.

2

u/[deleted] Jul 16 '13

Whether or not you see a point, it's the recommended way to use it. One point that I appreciate is that you can do your development without a build step in between changes, but then still deploy an optimized version by using r.js.

0

u/gatorcoder Jul 16 '13

Also if I need to debug the code that exists in module 220.js I go to the file in my debugger instead of a MASSIVE combined version that CJS provides. There are certainly trades offs. I hope you don't have a page with 200+ modules.

1

u/medikoo Jul 16 '13

Wrong. You edit each files individually as with AMD approach, you should never edit the bundle.

Additionally you can take advantage of source-map feature and see modules as individual files in a browser.

3

u/gatorcoder Jul 16 '13

I understand that you don't edit the bundle. Source maps are great but I have to test in IE9/10 and that doesn't support source maps. I really prefer the CJS syntax over AMD and I understand there are ways to get CJS in to the browser (you showed us one with your webmake library), but there are some shortfalls that unfortunately with my team/project it just isn't really in the cards, perhaps one day.

1

u/rhysbrettbowen Jul 17 '13

the point is you can DEVELOP without a build cycle. Just refresh the page and it works and then only build when you want to push it out. This means you're working on the actual files and can debug them directly in the browser without a sourcemap.

0

u/[deleted] Jul 16 '13

[deleted]

1

u/medikoo Jul 16 '13

There's no single point of using AMD if you bundle it.

1

u/rhysbrettbowen Jul 17 '13

the point is you can DEVELOP without a build cycle. Just refresh the page and it works and then only build when you want to push it out. This means you're working on the actual files and can debug them directly in the browser without a sourcemap.

1

u/medikoo Jul 18 '13

Exactly, and this benchmark compares both systems as used in Development mode.

However, if someone states that in development with AMD he still relies on build step (and that such way should be used for comparison), I'm asking why he uses AMD (?) as then there's no single advantage of choosing that system.

Hope that's clear now.

4

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

u/[deleted] 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).