简体   繁体   English

将新代码注入DOM的最高效方法是什么?

[英]What is the most performant way to inject new code into the DOM?

I am engaged in a small debate with the server side developer for my project (I'm the front end guy) around injecting new stuff into DOM. 我正在与服务器端开发人员就我的项目(我是前端人员)进行一场小型辩论,围绕向DOM注入新内容。 He maintains that the best method to inject a large amount of code (received via ajax) is to send a JSON object and then to iterate through each item of that object. 他认为注入大量代码(通过ajax接收)的最佳方法是发送一个JSON对象,然后遍历该对象的每个项目。 He says that will save some bandwidth and is more server friendly. 他说,这将节省一些带宽,更加服务器友好。

Obviously (for me, at least :-) ), this mean a LOT of cpu cycles on client. 显然(对我而言,至少:-)),这意味着客户端上有很多cpu周期。

The data is basically a table with 20-30 rows (2-3 cols each), which means few (useless) iterations. 数据基本上是一个包含20-30行(每个2-3列)的表,这意味着很少(无用)迭代。

On the other hand, I think that the best method is to send pure XHTML (server generated source) and just inject it in place. 另一方面,我认为最好的方法是发送纯XHTML(服务器生成的源)并将其注入到位。 This means just one cpu cycle ( $('selector').html(data) where data is the data received with AJAX, but also means a lot of bloated HTML code. 这意味着只有一个cpu周期( $('selector').html(data) ,其中data是用AJAX接收的数据,但也意味着很多臃肿的HTML代码。

I use jQuery (but I think that is not too important). 我使用jQuery(但我认为这不太重要)。

So, what do you think, guys? 所以,你觉得怎么样?伙计们? Thanks! 谢谢!

$('selector').html(data) is not one cpu cycle; $('selector').html(data)不是一个cpu循环; it's one method call. 这是一个方法调用。 The browser has to process all that HTML. 浏览器必须处理所有HTML。

The only way to tell is to test both methods and decide how much you care about client response time vs. server load. 唯一的方法是测试两种方法,并决定您关心客户响应时间与服务器负载的关系。 From your description, it probably doesn't makes much difference either way. 根据您的描述,它可能没有太大的区别。 I suspect you'll want to optimize for a third factor: developer comfort. 我怀疑你会想要优化第三个因素:开发人员的舒适度。 Do whichever makes the most sense and is easy to maintain. 做任何最有意义且易于维护的。

Even as at first I commonly send HTML to be inserted at specific places, now I tend to use JSON whenever I can, XML otherwise, mainly because I can modify a lot of places of the page, and act more dynamically with little information. 即使起初我通常发送HTML以插入特定的地方,现在我倾向于随时使用JSON ,否则就是XML ,主要是因为我可以修改页面的很多地方,并且用很少的信息动态地动作。

Also, JSON has a smaller footprint than XML and is also usually better "human parseable". 此外, JSON的占用空间比XML小,通常也更好“人类可解析”。 XMl's biggest advantage is that it has been around for a long time and is the standard, so you have both the tools, and the knowledgeable workforce at hand for it. XMl的最大优势在于它已经存在了很长时间并且是标准配置,因此您可以拥有这些工具和知识渊博的劳动力。 JSON, on the other hand is a little more obscure . 另一方面,JSON更加模糊 Of course, any developer worth its paycheck will be able to learn it faster than I can write this. 当然,任何值得支付薪水的开发人员都能比我写这篇文章更快地学习它。 Just think about it: 考虑一下:

  • it has javascript syntax and 它有javascript语法和
  • its the same concept of XML. 它与XML的概念相同。

About just sending HTML Quirksmode says: 关于发送HTML Quirksmode说:

If the HTML snippet contains forms, or if the receiving HTML element is a form, this method give horrific errors in Explorer. 如果HTML代码段包含表单,或者接收HTML元素是表单,则此方法会在资源管理器中显示可怕的错误。

(...) (......)

I'm going to study JSON carefully and might switch to it for an unrestricted access application I have in mind. 我将仔细研究JSON,并且可能会切换到我想到的无限制访问应用程序。 Nonetheless I feel that XML remains the best overall format for the time being, mainly because people are used to it. 尽管如此,我认为XML仍然是目前最好的整体格式,主要是因为人们习惯了它。

In addition, HTML snippets may become quite complicated (...) Thus the server side script that generates the HTML may become quite complicated. 此外,HTML片段可能会变得非常复杂(...)因此,生成HTML的服务器端脚本可能会变得非常复杂。

Furthermore, by sending just HTML you can just paste information, you won't be getting information, you'd be getting snippets, so you couldn't operate with it. 此外,通过将只用HTML你可以粘贴的信息,你将不会得到信息,你会得到的片段,所以你不能用它工作 Remember that the advantage of AJAX is to have dynamic pages , not pages that have some parts that update without reloading the full page . 请记住, AJAX的优势在于拥有动态页面 ,而不是具有更新某些部分但不重新加载整页的页面 You can use it for that, and is OK, and a valid use, but you are under utilizing the potential. 可以使用它,并且可以使用,但是您正在利用潜力。

Even when inserting HTML could be faster than dom manipulation, I don't think that its that much (besides the problems you could have with IE 6). 即使插入HTML 可能dom操作更快,我也不认为它那么多(除了IE 6可能存在的问题)。 This you should test and see if for your use, its really a bottle neck of performance. 你应该测试一下,看看是否适合你的使用,它真的是一个性能瓶颈。

I tend to agree with the closing argument of the previous link . 我倾向于同意前一个链接的结束论点。

Although I'd love to be able to say that one of them is "the best", I think choosing the right format depends on the circumstances, and not on any theoretical musings. 虽然我很乐意能够说其中一个是“最好的”,但我认为选择正确的格式取决于具体情况,而不是任何理论上的思考。

It depends on the data that's being returned, in most of the situations I find myself in. If the server is replying with a form, or general content, I prefer the straight XHTML as you do. 它取决于返回的数据,在我发现自己的大多数情况下。如果服务器回复表单或一般内容,我更喜欢直接的XHTML。

In the event I need to receive actual data , however, I prefer the JSON format. 但是,如果我需要接收实际数据 ,我更喜欢JSON格式。 Yes, I have to generate the markup on the client side, but since everything is nicely organized in the JSON array I can pick and choose and filter as needed. 是的,我必须在客户端生成标记,但由于JSON数组中的所有内容都很好,我可以根据需要选择和过滤。 This also lets me easily change the view of the data later on, without having to get our back-end guy involved (his to-do list is always 4-5 days long!) 这也让我可以在以后轻松更改数据视图,而无需让我们的后端人员参与其中(他的待办事项列表总是4-5天!)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM