简体   繁体   English

有多少div标签太多了?

[英]How many div tags are too much?

How many div tags in one HTML document would one need before it affects performance? 在影响性能之前,一个HTML文档中需要多少个div标签? In this case, the tags are not nested and the content within each is minimal (background color/image). 在这种情况下,标签不是嵌套的,并且每个标签内的内容都是最小的(背景颜色/图像)。

This question is a follow-up on a previous question; 这个问题是对前一个问题的后续跟进; Drawing lines with clickable points using JavaScript 使用JavaScript绘制带有可点击点的线条

Here I settled on doing this using HTML and CSS. 在这里,我决定使用HTML和CSS来做这件事。

The <div> tags will have a width of at least 4 pixels, and a total width of 400-800 pixels, thereby 100-200 div tags. <div>标签的宽度至少为4像素,总宽度为400-800像素,因此100-200 div标签。

On top of that, there will be five or six of these graphs/timelines stacked on top of each other. 最重要的是,这些图表/时间线中将有五个或六个堆叠在一起。 The number of div tags is then up in 500-1200. 然后div标签的数量在500-1200之间。

Again, bearing that there is little content in each, how would this affect the performance? 再说一次,承认每个内容很少,这对性能有何影响?

_L _L

Test and find out. 测试并找出答案。 This will vary too much by rendering engine to answer generally. 通过渲染引擎来回答这一点会有很大的不同。

The actual displayed size of the div has no noticeable affect on performance. div的实际显示大小对性能没有明显影响。 Most of your performance is going to be lost in the transfer of the data to the browser and the rendering of all the elements. 在将数据传输到浏览器以及渲染所有元素时,大多数性能都会丢失。 It should be a relatively linear decrease in performance luckily, and not something that cascades quickly. 幸运的是,它应该是性能的相对线性下降,而不是快速级联的东西。

The actual limit doesn't really exist in concrete terms, its more of an issue of the power of the person's computer and browser and connection. 具体而言,实际限制并不存在,更多的是人的计算机和浏览器以及连接的权力问题。 Needless to say, you can get pretty big without causing major problems. 毋庸置疑,你可以在不引起重大问题的情况下变得非常大。

While tag count surely has an impact when rendering html you will have to benchmark to see whether or not the time is acceptable or not. 虽然标记计数在渲染html时肯定会产生影响,但您必须进行基准测试以确定时间是否可接受。

Where I would be concerned is javascript functions that you may have that try to traverse the DOM. 我会关注的是你可能尝试遍历DOM的javascript函数。 Looking through all those elements could be a costly operation on the client. 查看所有这些元素对客户来说可能是一项代价高昂的操作。 Though once again, proper benchmarking can not be substituted. 虽然再一次,正确的基准测试无法取代。

The answer depends on a lot of things. 答案取决于很多事情。 Every single div tag has some tiny impact on performance. 每个div标签对性能影响很小。 The tipping point for where performance starts to rapidly degrade will depend on the browser and computer viewing the page. 性能开始迅速降低的转折点取决于浏览器和查看页面的计算机。

It also matters what CSS rules you have affecting those styles. 您对影响这些样式的CSS规则也很重要。 Relatively speaking, some CSS selectors are fast to execute and some are slow to execute. 相对而言,一些CSS选择器执行速度快,有些执行速度慢。 If you have some slow CSS selectors defined, your tipping point will come much earlier that if you use no CSS or use only fast CSS selectors. 如果您定义了一些缓慢的CSS选择器,那么如果您不使用CSS或仅使用快速CSS选择器,那么您的临界点会更早出现。 Google's PageSpeed addon can give you insight as to whether you're using slow CSS selectors. 谷歌的PageSpeed插件可以让你深入了解你是否使用慢速CSS选择器。

Also, if you're doing things like animating them as a particle system, your tipping point will come even faster. 此外,如果您正在将它们设置为粒子系统,那么您的引爆点会更快。

If its anything like <font> tags, the answer is seemingly millions! 如果它像<font>标签一样,答案似乎是数百万! Check out: http://www.fujinonbinos.com/ (do a View Source - seriously!) 退房: http//www.fujinonbinos.com/ (做一个查看源 - 认真!)

Seriously, though, keeping things to a minimum is always good practice both for readability (code maintenance) and speed. 但是,严肃地说,将事情保持在最低限度对于可读性(代码维护)和速度来说总是很好的做法。 However, this is unlikely to make a serious impact unless its ridiculous. 但是,除非其荒谬,否则不太可能产生严重影响。 Even the above example isn't that slow! 即使上面的例子也不是那么慢!

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

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