java 脚本单独文件的性能受到影响?

发布于 2024-12-09 00:44:57 字数 46 浏览 1 评论 0原文

如果我将一些较长的客户端代码分解为单独的包含内容,这对客户端的性能影响有多大?

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

南风起 2024-12-16 00:44:57

对于初学者来说,大多数浏览器只允许来自同一域的 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.

吻泪 2024-12-16 00:44:57

我想客户端的初始请求会产生更多的开销,但除非脚本文件很大,否则我不认为这是一个真正的问题。然而,这里的好处是那些跨请求缓存的脚本文件。因此,如果您从 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文