java 脚本单独文件的性能受到影响?
如果我将一些较长的客户端代码分解为单独的包含内容,这对客户端的性能影响有多大?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
对于初学者来说,大多数浏览器只允许来自同一域的 6-8 个同时下载。因此,如果您有 10 个文件要加载,前 7 个文件将同时加载,当一个文件完成后,第 8 个文件将加载,依此类推...这就是大多数网站使用 CDN 的原因 - 这样他们就可以同时下载更多文件。最佳实践是在单独的文件中进行开发,但在生产部署期间,您将它们编译到同一文件中并运行 minify 等...这为您提供了两全其美的优势。
如果您坚持使用多个文件,请考虑将它们托管在不同的域(例如 Amazon S3,或者只是您网站之外的子域)上,以加快加载时间。不过,您应该使用不超过 5 个域进行加载,因为每个域都需要进行 DNS 查找,这可能会带来高昂的成本。您应该托管来自每个(子)域的多个文件,否则 DNS 查找的好处将被抵消。
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.
我想客户端的初始请求会产生更多的开销,但除非脚本文件很大,否则我不认为这是一个真正的问题。然而,这里的好处是那些跨请求缓存的脚本文件。因此,如果您从 100 个不同的网页引用特定的脚本文件,则该文件只会被下载和缓存一次。相比之下,将相同的脚本直接嵌入到 100 个不同的页面中,使用脚本文件可以节省大量成本。节省的金额取决于您的网站 JavaScript 的使用量。
另外,如果将脚本与内容分开,维护脚本就会容易得多。
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.