性能方面:请求JSON并在JS中渲染,或请求整个HTML? [英] Performance-wise: request JSON and render in JS, or request the entire HTML?
问题描述
如果我向PHP文件发送AJAX请求,什么会导致更快的HTML呈现:
- 直接从PHP发送完全格式化的HTML,或者:
- 发送JSON数据并让Javascript执行HTML呈现?
我有一个相当复杂的HTML结构,大块HTML块的时间与Javascript(jQuery)需要呈现相同结构的时间有关。
是否有确凿的答案?
JSON是要走的路。 网络可能是一个巨大的瓶颈,而javascript在处理事情上很快。最大的不同点在于连接速度缓慢。它绝对值得解析。新的浏览器提供了原生JSON ,所以它应该疯狂快速。
还有一件事要考虑: innerHTML有许多错误 (表格,表格等)。在这些情况下,您必须做很多开销才能使其跨浏览器。问题可能会出乎意料地发生,这会使您的应用程序不太稳定。然而,JSON允许您根据内容决定是否使用innerHTML或DOM方法。这是另一个巨大的胜利。
Possible Duplicate:
Why is it a bad practice to return generated HTML instead of JSON? Or is it?
IF I send an AJAX request to a PHP file, what would result in faster rendering of HTML:
- Sending the completely formatted HTML straight from PHP, or:
- Just send JSON data and let Javascript do the HTML rendering?
I have a rather complex HTML structure, and this puts download time of a large HTML chunk vs. the times Javascript (jQuery) needs to render the same structure.
Is there even a conclusive answer?
JSON is the way to go. Network can be huge bottleneck while javascript is fast at handling things. The greatest difference will be on slow connections. And it definitely worth the parsing. New browsers offer native JSON, so it should be crazy fast.
One more thing to consider: innerHTML has a lot of bugs (tables, forms, etc.). In those cases you have do a lot of overhead in order to get it work cross-browser. Problems may arise unexpectedly, which makes your application less stable.
JSON, however, let you decide if you want to use innerHTML or DOM methods according to the content. This is another huge win.
这篇关于性能方面:请求JSON并在JS中渲染,或请求整个HTML?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!