使用大量 ID 标签是否可以提高性能?
正如标题中提到的,我想了解在 html 中使用大量 ID 标签时是否真的存在一些性能开销。
我知道 CLASS 和 ID 之间的区别,但我不确定它们的性能。据我所知,ID 对于 JS 和浏览器都有特定的功能。浏览器将它们存储在内存中的某个位置,JS 使用它从浏览器内存中获取它们,从而比遍历整个源代码来搜索特定类更快地访问它们。
那么,如果我不需要使用 JS 或其他任何东西访问 ID,那么在 HTML 标记中使用它们是否合理?
As mentioned in title, I want to find out if there is really some performance overhead when using a lot of ID tags in html.
I know the difference between CLASSes and IDs, but I'am not sure about their performance. As far as I know, IDs have specific functionality both for JS and Browser. Browser stores them in it's memory somewhere, and JS uses it to get it from Browser's memory to access them much faster than traversing the whole source code in searching of a specific CLASS.
So, if I don't need to access the ID's with JS or anything else, will be it reasonable to use them in HTML markup?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这个问题的详细答案(如上所述)是“这取决于”。
这取决于:
您对“很多”的定义。
有些人会考虑很多,有些人会考虑 1000,另一些人会考虑 10,000
使用哪个浏览器以及浏览器的版本。
正在使用的机器、CPU 的速度等。
正在使用的操作系统。
当时的互联网(区域/本地)速度可以下载所有 div 标签。
div 的位置以及是否有任何页面可以在没有它们的情况下加载。
结论:- 鉴于我们正在讨论 Web 应用程序以及基于用户客户端的许多差异,因此如果可能,请保持较低的 div 数量。
The detailed answer to this question (as stated) is "it depends".
It depends on:
Your definition of 'a lot'.
Some folks would consider 100 a lot, others a 1000, others 10,000
Which browser being used and the version of the browser.
The machine being used, how fast the cpu is, etc.
The OS being used.
The internet (regional/local) speed at that time to download all the div tags.
Where the div's are and if any of the page can load without them.
In conclusion:- given we're talking about web apps and many differences based on user clients, keep the number of div's low if possible.
我从未注意到由于 HTML 标记中使用了 id 的数量而导致性能下降。
在我看来,会降低性能的因素是使用客户端脚本来操作大量分配给 ID 的 HTML。
这是我根据经验得出的看法。尚未完成关于该主题的研究。
I have never noticed a degradation of performance as the number of id's are used in HTML tags.
The thing that would degrade performance in my opinion would be the use of client side scripting to manipulate the HTML the ID's are assigned to in mass.
This is my opinion based on experience. No research completed on the subject.