Is it wise to use an AMD loader like RequireJS with a PhoneGap/Cordova application?

cordova, requirejs

Solution

If you are using r.js to at least concatenate (if not optimize, which is arguably of questionable benefit to some when all the assets are local) your modules, you will not be loading from the file system other than the initial load of your application. By using loader plugins such as "text", etc., even your HTML/template assets can be in-lined into modules in your concatenated file, so that the loading of a template is simply the "perf hit" of a function call. To address @Gajotres's point, you can either order the script tags and include Cordova first, OR shim it via require.config if you're require.js 2.1.0 (I believe) or better. So - in a nutshell, if you use require.js for hybrid mobile, DON'T skip the concatenation step. Assuming you've concatenated your modules into a "built" js file, at that point performance is going to depend both on factors beyond your control (device, native webview implementation, etc.) and other factors you can control... things like:

- what's the overal complexity of the app? If it's static views, then showing/hiding already-present DOM elements will be faster than requiring a module that requires a template module and renders the template using whatever template engine, etc.

- assuming it's not static views, deeply nested DOM structures (for one example) can give you grief on performance, reflow, etc.

On the whole, Andrew Trice has a great piece on UX considerations for mobile: http://www.tricedesigns.com/2013/03/11/performance-ux-considerations-for-successful-phonegap-apps/

If the app isn't terribly complex, then a simple build step that concats your hand-rolled modules (so you get the advantage of lower cognitive overhead while you develop, plus the build output of one file) might be sufficient. But unless you're concatenating templates, too (in a js module-accessible format), you'll have to load them at some point...

Problem

We all know the benefits of using RequireJS. I'm wondering though whether there are performance considerations to take into account when developing a Cordova app (especially on a platform like Android 2.x which can be dog slow). Say I'm writing a SPA and dynamically loading a module when navigating to a new view - won't there be latency issues, even if I am loading from the file system? As opposed to loading all of my JavaScript assets when the app initially loads? Yes, I know I could test myself - I'm just wondering whether anyone out there has tested the performance already!

Original source