Performance hit on java-script separate files? [closed]
If I break up some of my longer client side code into separate includes, how much of a performance hit is that for the client?
Well for starters most browsers only allow 6-8 simultaneous downloads from the same domain. So if you have 10 files to load, the first seven will load at the same time, when one completes the 8th will load and so on... This is why most sites use CDNs - so they can download more files at the same time. A best practice is to develop in separate files but during your production deployment you compile them into the same file and run minify etc... this gives you the best of both worlds.
If you insist on multiple files then look into hosting them on different domains (maybe Amazon S3 for instance, or just a subdomain off of your website) to speed up your load times. You should use no more than 5 domains for loading, though, as each domain requires a DNS lookup which can be costly. You should be hosting more than a couple files from each (sub)domain otherwise its benefit is outweighed by the DNS lookup.
I'd imagine there would be a bit more overhead on the initial request from the client, but unless the script files are huge, I don't see that being a real problem. However, the benefit here is going to be from those script files being cached across requests. So if you're referencing a particular script file from 100 different web pages, it will only be downloaded and cached once. Compare that to having the same script embedded directly in 100 different pages and there could be a nice saving overall from using script files instead. How much that saving will be will depend on how JavaScript-heavy your site is.
Plus it'll be far easier to maintain your scripts if you separate them out from your content.
精彩评论