在大型网站上平衡服务器端和客户端代码
假设我正在为 Facebook(我不是)或其他一些流量较大的网站编程。
我们的一般布局是:
- 所有 CSS 都在外部文件中
- 99% 的 JS 在外部文件中
- 网站的主干 HTML 由 PHP/MySQL 后端生成。
- JS 函数生成经常更改的部分的 DOM。
- 我们的服务器有一个就地 API,它返回 JSON 对象,这些对象被馈送到我们的 DOM 构建器(JS 函数),该构建器采用一个 JSON 对象数组,比如说朋友列表,然后生成用于显示朋友列表的所有 html,或事件等。
问题
- 这合理吗?
- 正如我所提到的,使用专用的 JS 函数来构建 DOM 是常见做法吗?
- 这是可扩展的吗? JS 太慢了吗? (顺便说一句,我们几乎只使用 JQuery)
我知道它极大地降低了带宽和服务器负载,因为服务器不再迭代好友列表(它也通过 MySQL 查询该列表)并生成所有 HTML,而是进行查询并返回一个小的 JSON 对象。这对我来说似乎没问题,但我想要第二/第三/第四/...意见。
多谢!
如果我遗漏了任何重要信息,请告诉我。
Let's suppose I am programming Facebook (which i'm not), or some other site that involves heavy traffic.
Our general Layout is:
- All CSS is in external files
- 99% of JS is in external files
- The back bone of the websites HTML is generated by a PHP/MySQL backend.
- JS functions to generate the DOM of parts that commonly change.
- Our server has an inplace API that returns JSON objects that are fed to our DOM builders (JS functions), which take an JSON object array of lets say, a list of friends, then generated ALL the html used for displaying a list of friends, or events, etc.
Question
- Is this reasonable?
- Is it common practice to have dedicated JS functions to build the DOM as I have mentioned?
- Is this scalable? Is JS too slow? (btw, we use JQuery almost exclusively)
I know that it tremendously lowers bandwidth and server load, as the server is no longer iterating through a list of friends (of which it also queried for via MySQL) and generating all the HTML, but instead making a query and returning a small JSON object. which seems ok to me, but I would like a 2nd/3rd/4th/... opinion.
Thanks alot!
Let me know if I am missing any critical information.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为你的布局非常好,但我仍然建议你阅读 YAHOO!加快网站速度的最佳实践。它比我更好地总结了它(在我看来非常好读)。
我认为 facebook 为了实现其规模而做的三件最重要的事情:
其他一些有趣的链接:
I think your layout is pretty good, but I would still advice you to read YAHOO! Best Practices for Speeding Up Your Web Site. It summarizes it better(very good read in my opinion) than I could do.
I think the three most important things facebook does to achieve it's scale:
Some other interesting links:
鉴于 JS 引擎正在不断开发以提高性能,我认为这是一个非常好的设计。我们将越来越频繁地将其视为 HTML5 和 HTML5。 Co 越来越受欢迎。
Given that JS engines are being constantly developed towards improved performance, I think it's a pretty good design. We will be seeing it more and more often as HTML5 & Co gains popularity.
这就是我正在采取的方法。我对此很满意。 JS当然不会太慢。您需要一个好的 JS 模板系统。 Trimpath JS 模板是我尝试过的模板中我最喜欢的。
This is the approach I'm moving towards. I'm pretty happy with it. JS is certainly not too slow. You'll want a good JS templating system. Trimpath JS Templates is my favorite of those I've tried.