javascript performance [closed]
I've just been given the following feedback on a staging site I have.
There are over 100 lines of JavaScript code [in the footer] which should be in a separate file for performan开发者_运维技巧ce, not in the markup.
Although I was under the impression that serving it in the footer was a performance boost given that it is one less http request.
Which is the better method (inline or external) for a production environment?
Keeping the JS in an external file allows it to be cached and reused between pages (or reloads of the same page).
There is a small performance penalty at the time of first load in exchange for a (relatively) big enhancement in future.
Having it in a separate file allows for caching, hence the suggestion.
Inline JavaScript is more performant for a single page request, but an external JS file is better code management, and gains on performance for repeat requests due to browsers being able to cache the file.
No caching is able to be done if the JS is inline, which means that you'll be sending the same data over and over, wasting your bandwidth, and your users' bandwidth.
The JavaScript code will probably not change over different. Moving these static lines of code to an external file allows caching of the file, so that pages can be load faster.
A few lines of code may not be worth moving to an external file, but hundreds of lines will certainly outweight the size of the overhead of a request (headers).
精彩评论