Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does minified JavaScript code improve performance?

Tags:

javascript

I'm making an AIR application (so the download time doesn't have a huge impact). Does combining and minifing all the JavaScript files affect the performance? How would obfuscating affect the performance?

like image 935
brian14708 Avatar asked Jul 25 '09 07:07

brian14708


People also ask

How much faster is minified JS?

In some cases, minification can reduce file size by as much as 60%. For instance, there's a 176 kb difference between the original and minified version of the JQuery JavaScript library.

Should you minify your code?

After the code is written, minifying software can be used in order to improve performance. This is because browsers can execute code without needing to understand it.

Does minified HTML run faster?

Now, because minification removes unnecessary characters from these HTML, CSS, and JS files, the size of these files get smaller. This in turn results in faster downloading and faster rendering of these files. Therefore minification can help improve your website speed.

Why should you minify your JavaScript scripts before publishing them?

These characters include whitespaces, line breaks, comments, and block delimiters which are useful for us humans but unnecessary for machines. We minify the files of a website containing CSS, HTML, and Javascript code so your web browser can read them faster.


2 Answers

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

Neither minifying nor obfuscating alter the 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 its plugins) can yield even greater savings.

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

like image 196
Gabriel Hurley Avatar answered Sep 26 '22 08:09

Gabriel Hurley


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 be explicitly ignored (since they're not there).

Combining

I've written quite a few HTML/JavaScript AIR applications, and from personal experience, combining files won't make a difference. In fact, it's good practice to separate the script based on certain criteria (classes, global functions, SQL functions, etc.). It 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.

like image 20
aditya Avatar answered Sep 22 '22 08:09

aditya