使用php加载脚本效率如何?
我有一个大约 10-12 页的网站,整个网站都使用 jQuery/Javascript。由于并非每个页面都需要所有脚本,因此我目前使用 switch
语句在任何给定页面上仅输出所需的 JS,以减少请求数量。
我的问题是,就性能而言,效率如何?如果不是,是否有其他方法可以选择性地在页面上仅加载所需的 JS?
I have a website that's about 10-12 pages strong, using jQuery/Javascript throughout. Since not all scripts are necessary in each and every page, I'm currently using a switch
statement to output only the needed JS on any given page, so as to reduce the number of requests.
My question is, how efficient is that, performance-wise ? If it is not, is there any other way to selectively load only the needed JS on a page ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这可能根本没有必要。
请记住,如果您的缓存设置正确,嵌入 JavaScript 仅在第一次加载时花费时间 - 每个后续请求都将来自缓存。
除非有很大的例外(例如,特定页面使用巨大的 JS 库),否则我会考虑始终嵌入所有内容,也许使用缩小功能,以便所有内容都在一个小文件中。
不过,我没有发现您使用的方法有任何性能问题。毕竟,这是决定是否输出一行代码。从长远来看,使用最具可读性和可维护性的方法。
This may not be necessary at all.
Bear in mind that if your caching is properly set up, embedding a JavaScript will take time only on first load - every subsequent request will come from the cache.
Unless you have big exceptions (like, a specific page using a huge JS library), I would consider embedding everything at all times, maybe using minification so everything is in one small file.
I don't see any performance issues with the method you are using, though. After all, it's about deciding whether to output a line of code or not. Use whichever method is most readable and maintainable in the long term.
由于您已经在使用 JS,因此您可以完全使用 JS 解决方案 - 例如您可以使用 yepnope 而不是 php。我不知道您网站的结构是什么,以及如何确定哪个页面需要什么或在什么时候包含某些内容(加载时,在某些远程事物完成传递数据后),但是如果您广泛使用 $.ajax,您还可以使用 yepnope 来提取 $.ajax 完成其预期功能后所需的额外 JS。
Since you're using JS already, you can use JS solution completely - for example you could use yepnope instead of php. I don't know what's the structure of your website and how you determine which page needs what or at what point is something included (on load, on after some remote thing has finished delivering data), however if you use $.ajax extensively, you could also use yepnope to pull additional JS that's needed once $.ajax is done with what it was supposed to do.
您可以放心地假设 JavaScript 已正确缓存在客户端。
因为我还假设您提供缩小的文件,所以看到您网站的大小,我认为性能可以忽略不计。
You can safely assume the javascript is properly cached on the clientside.
As I also assume you serve a minified file, seen the size of your website I'd say the performance is neglectable.
最好将所有 JavaScript 放在一个单独的“.js”文件中,并在页面中引用该文件。
原因是浏览器将有效地缓存该文件,并且每个会话仅下载一次(或更少!)。
唯一的缺点是,如果更改脚本,则需要“刷新”几次。
It is much better to place ALL your JavaScript in a single separate ".js" file and reference this file in your pages.
The reason is that the browser will cache this file efficiently and it will only be downloaded once per session (or less!).
The only downside is you need to "refresh" a couple of times if you change your script.
因此,经过一番修改后,我决定尝试一下LABjs。它确实工作得很好,而且我的代码也因此变得不那么臃肿。考虑到我的网站的大小,性能没有明显的提高,但代码现在更易于维护。
有趣的是,我的标题中有一个类似 Facebook 的按钮。在分析了 Firebug 中的请求后,我决定将其删除,并且页面加载时间缩短了惊人的 2 秒。天哪,这该死的东西是无用的...
谢谢大家的回答!
So, after tinkering a bit, I decided to give LABjs a try. It does work well, and my code is much less bloated as a result. No noticeable increase in performance given the size of my site, but the code is much, much more maintainable now.
Funny thing is, I had a facebook like button in my header. After analyzing the requests in firebug I decided to remove it, and gained an astounding 2 seconds on the pages loading time. Holy crap is this damn thing inneficient...
Thanks for the answers all !