Does minified javascript improve performance?

后端 未结 8 1652
渐次进展
渐次进展 2020-11-27 05:26

I\'m making an AIR application (so download time doesn\'t have a huge impact), does combining and minifing all the javascript files affect the performance? How would obfusca

相关标签:
8条回答
  • 2020-11-27 05:40

    Everyone here already talked about minifying but nobody talked about the 2nd part of your question - combining. This will definitely improve performance, probably even more than minifying.

    Multiple files require multiple HTTP requests, so when you put them all into one file, only 1 request is needed. This is important for 2 reasons:

    • each individual HTTP request may take longer to load for various routing reasons, and 1 file will potentially delay your whole application.
    • browsers and other clients have a max limit of files they are allowed to download concurrently from a single domain. Depending on the number of files in your application, this may mean the client queuing them up, thus making the load even longer.

    Also, besides minifying and combining, you have to absolutely make sure you have some sort of server side compression enabled. This can save you 90% or even more in the amount of bytes transferred, depending on the files.

    You can read more about compression (gzip, deflate) here: http://beerpla.net/2009/06/09/how-to-make-your-site-lightning-fast-by-compressing-deflategzip-your-html-javascript-css-xml-etc-in-apache/.

    0 讨论(0)
  • 2020-11-27 05:45

    Minification

    Minification does improve performance for two reasons:

    • Reduced file-size (because it removes comments and unnecessary white-spaces), so your script loads faster. Even if it is embedded into the <head>.

    • It is parsed faster, since comments and white-spaces don't have to explicitly ignored (since they're not there).

    Combining

    I've written quite a few HTML/JS AIR apps, and from personal experience, combining files won't make a difference. In fact, it's good practice to separate script based on certain criteria (classes, global functions, SQL functions etc). Helps keep them organised when the project becomes too big.

    Obfuscation

    Obfuscating is usually a combination of minification and renaming variables. It involves using eval to blow up the code again. This reduces performance for obvious reasons, but it depends on the size of your code.

    I'd suggest running tests to understand this best for your specific situation.

    [Edited to include special consideration for AIR apps]

    0 讨论(0)
  • 2020-11-27 05:46

    Minifying strips out all comments, superfluous white space and shortens variable names. It thus reduces download time for your JavaScript files as they are (usually) a lot smaller in filesize. So, yes it does improve performance.

    The obfuscation shouldn't adversely affect performance.

    Here's an article on the YDN that talks about minifying.

    0 讨论(0)
  • According to this page:

    Minification in Javascript is the process of removing all characters that are not necessary from the Javascript source code. That is why it is called “minification” – because all of the data that is not necessary to the functioning of the Javascript is removed from the source code, and therefore the Javascript is “minimized”. Even though these characters are removed from the Javascript source code, the functionality of the Javascript code does not change at all. So, your Javascript code will behave exactly the same even after it goes through the minification process. Code that has gone through the minification process is also known as “minified” code

    What are the benefits and advantages of Javascript minification

    The main purpose of Javascript minification is to speed up the downloading or transfer of the Javascript code from the server hosting the website’s Javascript. The reason that minification makes downloads go faster is because it reduces the amount of data (in the minified Javascript file) that needs to be downloaded. Less data means that the user’s browser spends less time processing that data, which is why time is saved. So, we can say that minification is performed on Javascript source code because it is essentially a performance enhancement – and it allows websites that use minified Javascript to load faster.

    0 讨论(0)
  • 2020-11-27 05:53

    Minifying improves performance for your page overall by decreasing the load time (even if only slightly).

    Neither minifying nor obfuscating alter execution time by any perceivable amount for the vast majority of javascript code out there.

    I do recommend minifying for those reasons and more. Minifying multiple scripts together (like jQuery and it's plugins) can yield even greater savings.

    Edit: As pointed out, on constrained devices and/or with very large codebases minifying could yield a noticeable result.

    0 讨论(0)
  • 2020-11-27 05:53

    I can't reply to the comment made by John, regarding cutting parsing time not only in half, but a 3rd (from 12s to 4s), when minifying a file from 3MB to 1MB, but I have serious questions when it comes to the truthy-ness of this statement, even if it was written in 2017.

    When reading about for example on SpiderMonkey, Firefox's Javascript Engine, there seem to be pretty much documentation that could explain a performance penalty on minifying javascript source code. And this has everything to do with Lazy Parsing or deferring parsing of a function until it's ultimately needed. This would spread parsing out over the full execution time T, while it seems to me, that only use case where minifying possibly could improve parsing, is if the entire project is parsed in one go-through (which also seems to be a worst practices kind of design).

    https://blog.mozilla.org/javascript/

    0 讨论(0)
提交回复
热议问题