DOM中安全的最大节点数? [英] Safe maximum amount of nodes in the DOM?

查看:215
本文介绍了DOM中安全的最大节点数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于一个Web应用程序,给定运行目标移动浏览器 2 的目标移动设备 1 中的可用内存,如何估计DOM节点的最大数量,包括可以通过HTML或DHTML生成的文本节点?

For a web application, given the available memory in a target mobile device1 running a target mobile browser2, how can one estimate the maximum number of DOM nodes, including text nodes, that can be generated via HTML or DHTML?

一个人怎么能之前计算出估算值

How can one calculate the estimate before

  • 失败
  • 崩溃
  • 响应速度明显下降

而且,对于任何浏览器,每次打开标签页时都没有硬限制吗?

Also, is there a hard limit on any browser not to cross per tab open?

关于事先关闭

这与以下评论中的其他问题不同.它还提出了一个非常具体的问题,寻求一种估算方法.没有关于它的重复,宽泛或观点,尤其是现在为了清楚起见对其进行了重写,而没有改变其作者的表达兴趣.

This is not like the other questions in the comments below. It is also asking a very specific question seeking a method for estimation. There is nothing duplicated, broad, or opinion based about it, especially now that it is rewritten for clarity without changing its author's expressed interests.

脚注

[1]例如,从2013年至2018年销售的具有特定RAM容量的Android或IOS移动设备

[1] For instance, Android or IOS mobile devices sold from 2013 - 2018 with some specific RAM capacity

[2] Firefox,Chrome,IE 11,Edge,Opera,Safari

[2] Firefox, Chrome, IE 11, Edge, Opera, Safari

推荐答案

这个问题,只有统计上的答案才是准确而全面的.

This is a question for which only a statistical answer could be accurate and comprehensive.

为什么

适当的等式是这样,其中N是节点数,字节 N 是在DOM中表示它们所需的总字节数,以及节点索引n ∈ [0, N).

The appropriate equation is this, where N is the number of nodes, bytesN is the total bytes required to represent them in the DOM, and the node index n ∈ [0, N).

bytes N = ∑ N (bytesContent n + bytesOverhead n )

bytesN = ∑N (bytesContentn + bytesOverheadn)

问题中要求的值是在最坏情况下的手持设备,操作系统,浏览器和操作条件下的N的最大值.为每个排列求解N并非易事.上面的等式揭示了三个依赖关系,每个依赖关系都可以大大改变答案.

The value requested in the question is the maximum value of N in the worst case handheld device, operating system, browser, and operating conditions. Solving for N for each permutation is not trivial. The equation above reveals three dependencies, each of which could drastically alter the answer.

  1. 节点的平均大小取决于每个节点用于容纳内容的平均字节数,例如UTF-8文本,属性名称和值或缓存的信息.
  2. DOM对象的平均开销取决于管理每个文档的DOM表示形式的HTTP用户代理. W3C的文档对象模型常见问题解答指出:尽管所有DOM实现都应该是可互操作的,但它们可能代码大小,内存需求和单个操作的性能差异很大."
  3. 可用于DOM表示的内存取决于默认情况下使用的浏览器(具体取决于浏览器手持设备供应商或用户喜欢的浏览器),默认浏览器的用户替代,操作系统版本,内存容量手持设备的数量,常见的后台任务以及其他内存消耗.
  1. The average size of a node is dependent on the average number of bytes used in each to hold the content, such as UTF-8 text, attribute names and values, or cached information.
  2. The average overhead of a DOM object is dependent on the HTTP user agent that manages the DOM representation of each document. W3C's Document Object Model FAQ states, "While all DOM implementations should be interoperable, they may vary considerably in code size, memory demand, and performance of individual operations."
  3. The memory available to use for DOM representations is dependent upon the browser used by default (which can vary depending on what browser handheld device vendors or users prefer), user override of the default browser, the operating system version, the memory capacity of the handheld device, common background tasks, and other memory consumption.

严格的解决方案

一个人可以运行测试以确定手持设备上使用的每个常见http用户代理的(1)和(2).可以通过配置Web服务器的日志记录机制(默认情况下不放置HTTP_USER_AGENT),然后剥离日志中除该字段之外的所有字段并计算每个值的实例,来配置任意给定站点的用户代理分布

One could run tests to determine (1) and (2) for each of the common http user agents used on handheld devices. The distribution of user agents for any given site can be obtained by configuring the logging mechanism of the web server to place the HTTP_USER_AGENT if it isn't there by default and then stripping all but that field in the log and counting the instances of each value.

将需要测试每个字符的字节数,以获取属性值和UTF-8内部文本(或其他编码),以获得明确的计算因子对(1).

The number of bytes per character would need to be tested for both attributes values and UTF-8 inner text (or whatever the encoding) to get a clear pair of factors for calculating (1).

可用内存也需要在各种常见条件下进行测试,这本身就是一个重大的研究项目.

The memory available would need to be tested too under a variety of common conditions, which would be a major research project by itself.

选择的N的特定值必须为零才能处理实际的最坏情况,因此将选择一定百分比的内容,节点结构和运行时条件的典型情况.例如,可以使用某种形式的随机原位(在正常环境条件下)研究来抽取病例样本,然后找到满足这些病例95%的N.

The particular value of N chosen would have to be ZERO to handle the actual worst case, so one would chose a certain percentage of typical cases of content, node structures, and run time conditions. For instance, one may take a sample of cases using some form of randomized in situ (within normal environmental conditions) study and find N that satisfies 95% of those cases.

也许可以通过上述方式测试一组案例,并将结果放置在表格中.这样可以直接回答您的问题.

Perhaps a set of cases could be tested in the above ways and the results placed in a table. Such would represent a direct answer to your question.

我想这将需要一位具有良好数学背景的出色移动软件工程师和一名统计专家,全职工作并且要花费大约四个星期的大量预算才能得出合理的结果.

I'm guessing it would take an excellent mobile software engineer with a good math background and a statistics expert working together full time with a substantial budget for about four weeks to get reasonable results.

更实用的估计

一个人可能会猜到最坏的情况.经过几天的研究和一些概念验证的应用程序,可以完善此建议.缺时间的话,这是一个很好的第一个猜测.

One could guess the worst case scenario. With a few full days of research and a few proof-of-concept apps, this proposal could be refined. Absent of the time to do that, here's a good first guess.

考虑一种允许DOM使用1 GB的手机,因为出于上述目的,正常工作条件使用4 GB中的3 GB.可以假设一个节点的平均内存消耗如下,以获得一个大致数字.

Consider a cell phone that permits 1 Gbyte for DOM because normal operating conditions use 3 Gbytes out of the 4 GBytes for the above mentioned purposes. One might assume the average consumption of memory for a node to be as follows, to get a ballpark figure.

  • 每个字符2个字节,每个节点40个字符的内部文本
  • 每个字符2个字节,每个10个字符的4个属性值
  • 每个字符1个字节,代表4个属性名称(每个属性4个字符)
  • C/C ++节点开销为160个字节

在这种情况下,N worst_case 是最坏情况下的最大节点数,

In this case Nworst_case, the worst case max nodes,

= 1,024 X 1,024 X 1,024
  / (2 X 40  +  2 X 4 X 10  +  1 X 4 X 4  +  160)

= 3,195,660 . 190,476.

但是,如果可以避免的话,我不会在具有300万个DOM节点的浏览器中构建文档.请考虑采用以下更常见的做法.

I would not, however, build a document in a browser with three million DOM nodes if it could be at all avoided. Consider employing the more common practice below.

常用练习

最好的解决方案是使用标准的HTTP设计技术,将其总数保持在N之下,并简单地将节点总数减少到可能的程度.

The best solution is to stay far below what N might be and simply reduce the total number of nodes to the degree possible using standard HTTP design techniques.

  • 减少在任何给定页面上显示的内容的大小和复杂性,这也改善了视觉和概念上的清晰度.
  • 从服务器请求最少的数据量,使用开窗技术推迟尚不可见的内容,或以计划良好的方式在响应时间和内存消耗之间取得平衡.
  • 使用异步调用来帮助实现上述极简主义.

这篇关于DOM中安全的最大节点数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆