5 ms·
It depends on why you're minifying, but there are good reasons to do so. I'd bet, as in guess/speculate without evidence, that the #1 reason people do it is for
by dahart 11y ago
It depends on why you're minifying, but there are good reasons to do so. I'd bet, as in guess/speculate without evidence, that the #1 reason people do it is for code obfuscation. Front end JavaScript delivers source code to your users and competitors alike, that can be scary when you're working hard on something you think people will like & copy.
Most here are talking about uglify which does very superficial minimization, but Google's closure compiler is in the mix too, and that provides static type checking, nested variable flattening, dead code removal, inlining optimizations, and more. These things are substantial, not marginal, especially the static type checking. It's closer to C++ compilation than it is to gzip.
Gzip can be used (and usually is) either way, and generally still has some benefit even on minified code. But no, gzip can't replace or provide the majority benefit of a minification pass, they're two mostly independent things done for mostly different reasons.
- qCOVET 11y agoI don't know if you could hide it from competition - it can be unminified ex with: http://jsbeautifier.org/ http://jsbeautifier.org/ I have tried this before and it worked.
- emidln 11y agoGoogle Chrome also does this out of the box (and has for awhile).
- leeoniya 11y agoit depends on how you minify. if you use Closure Compiler in Advanced mode it will rename and rescope pretty much everything except native methods and strings (making lots of globals cause it assumes you're compiling all page's code at once), it will eliminate unused code even from third party libs like jquery and do all sorts of stuff that make the beautified code very expensive to understand, follow and reverse and even then you may end up with only half a library that does only one specific task and the rest of the code would be missing. you have to do a bit of JSDoc annotations in some places to fix some breakages and restructure the code a tiny bit to get it to rename absolutely everything but the results are excellent.
- kuschku 11y agoSo, run http://www.jsnice.org/ http://www.jsnice.org/ over it?
- leeoniya 11y agotried it out and got an error saying it doesnt support ES5 getters/setters unless i change the language_in option. is there a way to do this? even so, there's nothing it can do to bring back eliminated code, meaningfully extract inlined functions and scope everything appropriately to help a reverser understand context. it would still take an immense amount of effort only to get back something very specific. the only thing you can probably get out of it is some largely mathematical chunks that cannot be restructured much, eg: rgbToHsv.
- tomjen3 11y agoYou can also just click the {} icon in Chromes debugger. All you will get is a lot of function a with arguments z d and f calling eq on z and passing it d, then returning the qln value of the result. Slightly more readable, yes, but still like debugging assembler.
- deleted 11y ago[deleted]
- kuschku 11y agoIt actually provides meaningful names, here is more about it: https://news.ycombinator.com/item?id=7836092 https://news.ycombinator.com/item?id=7836092
- weinzierl 11y ago> it will eliminate unused code even from third party libs like jquery and do all sorts of stuff that make the beautified code very expensive to understand To have that sort of optimisation you'll have to run the Closure compiler with ADVANCED_OPTIMIZATIONS enabled. Last time I checked that mode of compilation didn't work with JQuery and not with most other libraries. To use it with your own code you'll have to follow certain rules. I think most people just use uglify or the Closure compiler in SIMPLE_OPTIMIZATIONS, neither of which does the very advanced stuff of ADVANCED_OPTIMIZATIONS.
- arihant 11y agoTools like this become increasingly useless as your codebase gets larger. For example, unminify React and try mapping functions to unminified code. There is a lot to minification, these tools just undo cosmetic minification.
- kuschku 11y agohttp://www.jsnice.org/ http://www.jsnice.org/ can do that – giving everything meaningful names, checking types, etc.
- bpicolo 11y agoIt can't give "meaningful names" without real context, which it doesn't have. Slightly longer names that are easier to read? Sure.
- kuschku 11y agoYes, it can: https://news.ycombinator.com/item?id=7836092 https://news.ycombinator.com/item?id=7836092
- bpicolo 11y agoHere's part of google homepage js thrown at it (In reality tossed a whole file, but pastebin wont fit it all for free). It's still entirely unreadable. http://pastebin.com/VvEGZ8a3 http://pastebin.com/VvEGZ8a3 If it can infer most of it from OSS? Sure. But big web apps do a lot more.
- the_watcher 11y agoThere are a lot of people who are doing it because it's suggested by Pagespeed Insights if you have any unminified JS/CSS/HTML, as well.
- smithclay 11y agoAnother reason is that minified code + gzipped code is expected for third-party JavaScript widgets (social media buttons, in-app chat, ads, etc) because of legitimate developer concerns around page-size bloat.
- ryanolsonx 11y agoI think the main reason that it's a best practice for people to minify their JavaScript files is for a smaller download size for the client. With a smaller download size, the page is able to utilize JavaScript at an earlier point and be ready for the user to interact with the page. Minifying and gzipping JavaScript yields a greatly smaller file size than without either. Big win for performance.
- DanBC 11y agoJust checking: are you saying that minifying and gzipping is better than gzipping alone? Where better means smaller downloads for the client?
- blacksmith_tb 11y agoI read that to mean that gzip / deflate was nearly as effective, and so there was not much reason to minify if you were already compressing.
- noamyoungerm 11y agoWhile it is true that in practice, the size benefit of both compressing and minifying is optimal, the LZ77 algorithm is nearly the same as a minification algorithm, so running both has only marginal benefit.
- tomjen3 11y agoMost of those benefits result in code that is faster to execute, the resulting code is smaller than e.g uglifyjs, but the main benefit is that the runtime performance is much better.