26

I am currently starting to build an PhoneGap application, I know the advantages of minifying code for browsers already. However, I was wondering if it was worth minifying my HTML, JavaScript and CSS code for my PhoneGap application?

I would understand that the code run faster potentially faster?

Also if that is the case is there an application that will do it all for me? (When I say do it for me I mean take a directory on my machine and minify the code saving a .min.js version? or is it a simple file by file?)

Thanks in advance for any help

1

5 Answers 5

27

Well tickle my feet and call me grandma, but I have been minifying the javascript for my phoneGap app, and it most certainly improves performance.

Of course there is barely any difference in terms of the size of the application package. So the one time that a user downloads and installs the app, there is no real gain from minifying.

But every time the user runs the app, having minified javascript does make a difference. Even though the file is local, it still needs to be loaded into the 'browser', and smaller files are loaded faster and will thus start executing earlier.

And chances are you'll have a lot of javascript since most apps want to "do" something instead of just providing passive HTML content.

I'm considering minifying the phonegap.js file itself too. Any thoughts on that are highly appreciated.

2
  • When you say smaller files are loaded faster, are you refering to I/O performance? Or the browser loading process?
    – sgimeno
    Jun 17, 2014 at 19:03
  • 1
    Small files will have finished loading earlier than large files. I have no reason to believe they get a faster I/O rate from the browser. Big books take longer to read than small books :)
    – Wytze
    Jun 19, 2014 at 10:03
19

An answer from PhoneGap representative, copied from PhoneGap Community Forum :

"Not necessarily. JavaScript compression is most important for network performance - web applications that are requesting files from remote servers need to wait for the file to be downloaded, so having a smaller file makes the process a lot smoother.

What matters on mobile devices, and PhoneGap apps specifically, is memory, rather than network traffic. While you may get some memory benefits from running a minified JS file, both the original and the minified file are interpreted into the same code*, so the difference is negligible.

In most cases, minifying JavaScript is low on the list of priorities for a PhoneGap application."

13

In general it is not worth minifying PhoneGap application. Minifying is beneficial for web application as it reduces size of each request.

With PhoneGap all your HTML/JS/CSS will be bundled into application package (which is compressed btw.) and downloaded once on application install. So there won't be any gains in speed.

4
  • 1
    This is what I thought however, I thought it may be run by the engine faster? although this may be milliseconds
    – Nick White
    May 10, 2012 at 10:51
  • 1
    @NickWhite I don't think, that you will get any (at least measurable/noticable) gains such way. I would say - don't do it, as you will complicate your build/deployment process without gaining any worth.
    – PiRX
    May 10, 2012 at 11:28
  • 8
    I still think its worthwhile to minify the code - remember that minification ALSO does some level of obfuscation - so the end-result is that your minified js/css is no longer "readable" by humans, and since the apk file (for android) is just a zip with a different name, minification will ensure that your code/IP is protected somewhat from plain copy-paste coders. Jan 13, 2014 at 7:35
  • 2
    Minification (depending on which minifier you use, and how you configure it) often does a lot more than just minify code. It often removes dead code, and may implement other optimizations, too, which will improve performance and will obviously improve the memory foot print. Also, the time it takes to load a 2mb file versus a minified 500k file, for instance, is not insignificant on some systems. Aug 21, 2015 at 3:00
3

This might be a bit late, but if you are still interested in minifying your application's code, I made an NPM package that minifies your Javascript, CSS, and image files called cordova-minify (https://www.npmjs.org/package/cordova-minify).

My project is also on GitHub, so if you want to contribute and/or see what is under the hood, it's there - https://github.com/alastairparagas/cordova-minify. It's practically a Cordova Hook that calls some NPM package dependencies to do the respective compression.

3
  • 1
    This does not answer the question, which is whether minification is beneficial. Aug 21, 2015 at 3:02
  • I would say, if minification is not bothering your app's functionality, then it is always must thing. Sep 1, 2015 at 6:22
  • Have installed your minify-v2 - it hangs on cordova build with only 1 message "Starting minifying ..." - have waited about 30mins
    – Developer
    Feb 8, 2019 at 13:31
0

From my experience Javascript grows a lot more than html/css.

From here: https://reactjs.org/docs/add-react-to-a-website.html

Tip: Minify JavaScript for Production
Before deploying your website to production, be mindful that unminifed JavaScript can significantly slow down the page for your users.
...
If you don’t have a minification step for your scripts, here’s one way to set it up.

Which takes to https://gist.github.com/gaearon/42a2ffa41b8319948f9be4076286e1f3

In production, it is recommended to minify any JavaScript code that is included with your application. Minification can help your website load several times faster, especially as the size of your JavaScript source code grows.

Yes, it is recommended to minify.
The 2nd link above has simple commands on how to minify, using terser npm package.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.