什么更快,将HTML DOM节点保存为变量,还是使用getElementById? [英] What's faster, saving an HTML DOM node as a variable, or using getElementById?

查看:67
本文介绍了什么更快,将HTML DOM节点保存为变量,还是使用getElementById?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

将jtml dom节点的精确位置保存到js中的

变量或每次使用getElementById更快你需要访问

节点吗?


我想尽快让我的应用程序。我有大约10-20

id标签需要不时访问和修改。如果我存储了所有dom节点字符串

" document.node.child ...."这将是jvm缓慢执行的b / b
进入一个巨大的js阵列?


或者,如果每次我需要访问节点时必须使用getElementById()

,它会慢吗? br />

预先感谢您的帮助,

C

解决方案

< blockquote>
ct******@gmail.com 写道:


大家好,


将jtml dom节点的精确位置保存到js中的

变量是否更快,或者每次需要访问时使用getElementById节点?



访问一个已保存在变量中的节点会更快,

getElementById。


我想尽快让我的应用程序。我有大约10-20

id标签需要不时访问和修改。如果我存储了所有dom节点字符串

" document.node.child ...."这将是jvm缓慢执行的b / b
进入一个巨大的js阵列?



Javascript与Java虚拟机无关。他们俩共同的唯一一件事就是他们都以java开头。


另外,它也会是如果我每次需要访问节点时都必须使用getElementById()

,那会很慢吗?



如果你必须多次访问一个节点,那么getElementById方法

将比你存储节点时慢。 />


非常感谢您的帮助。

web.dev写道:

ct******@gmail.com 写道:


大家好,

将jtml dom节点的精确位置保存到js中的

变量或每次使用getElementById更快你需要访问节点的
吗?



访问已保存在变量中的节点会更快,因此可以避免检索节点所需的时间通过

getElementById。


我想尽快让我的应用程序。我有大约10-20

id标签需要不时访问和修改。如果我存储了所有dom节点字符串

" document.node.child ...."这将是jvm缓慢执行的b / b
进入一个巨大的js阵列?



Javascript与Java虚拟机无关。他们俩共同的唯一一件事就是他们都以java开头。


另外,它也会是如果我每次需要访问节点时都必须使用getElementById()

,那会很慢吗?



如果你必须多次访问一个节点,那么getElementById方法

将比你存储节点时慢。


ct******@gmail.com 写道:


大家好,


保存html dom节点的精确位置是否更快在js中输入一个

变量,或者每次需要访问节点时使用getElementById




这取决于您打算访问节点的频率。如果它在紧密的循环中少于几百美元,那么它很可能没有明显的差别。


我'建议测试它以找出引爆点的位置,然后确定你是否有可能接近它。


我想尽快让我的申请。我有大约10-20

id标签需要不时访问和修改。如果我存储了所有dom节点字符串

" document.node.child ...."这将是jvm缓慢执行的b / b
进入一个巨大的js阵列?



我不认为20项的数组是巨大的。 20,000很大。


或者,如果每次我需要使用getElementById()

,它也会很慢访问节点?



" Slow"是相对的。如果您不经常访问节点,那么

它可能与感知速度没有区别。


如果不了解更多信息,很难推荐特定方法

关于它的用途,但一般我更喜欢在数组中存储

引用。我更喜欢使用

来识别要存储的节点,而不是说一系列ID(el-0,el-1等),或者

列表作为来自其他来源的数组提供。


对我而言,将div中的元素包装起来要容易得多,并且

传递其ID到一个函数,然后使用getElementsByTagName或类似于

找到我想要的ndo(可能进一步通过CSS类名称区分)

并存储对它们的引用。对于某些节点,您可以使用通用名称

属性和getElementsByName,但这只适用于

元素的小子集。

- -

Rob


Hi Everyone,

Is it faster to save the precise location of an html dom node into a
variable in js, or to use getElementById everytime you need to access
the node?

I want to make my application as fast as possible. I have about 10-20
id tags that need to be accessed and modified from time to time. Would
the jvm perform slowly if I stored all of the dom node strings
"document.node.child...." into a huge js array?

Alternatively, would it also be slow if I had to use getElementById()
everytime I needed to access the node?

Thanks in advance for the help,
C

解决方案


ct******@gmail.com wrote:

Hi Everyone,

Is it faster to save the precise location of an html dom node into a
variable in js, or to use getElementById everytime you need to access
the node?

It would be faster to access a node that you have saved in a variable,
thus avoiding the time it takes to retrieve the node through
getElementById.

I want to make my application as fast as possible. I have about 10-20
id tags that need to be accessed and modified from time to time. Would
the jvm perform slowly if I stored all of the dom node strings
"document.node.child...." into a huge js array?

Javascript has no relation with Java Virtual Machine. The only thing
they both have in common is that they both start off with "java".

Alternatively, would it also be slow if I had to use getElementById()
everytime I needed to access the node?

If you had to access a node many times, then getElementById method
would be slower than if you had stored the node.


Thank you so much for the help.
web.dev wrote:

ct******@gmail.com wrote:

Hi Everyone,

Is it faster to save the precise location of an html dom node into a
variable in js, or to use getElementById everytime you need to access
the node?


It would be faster to access a node that you have saved in a variable,
thus avoiding the time it takes to retrieve the node through
getElementById.

I want to make my application as fast as possible. I have about 10-20
id tags that need to be accessed and modified from time to time. Would
the jvm perform slowly if I stored all of the dom node strings
"document.node.child...." into a huge js array?


Javascript has no relation with Java Virtual Machine. The only thing
they both have in common is that they both start off with "java".

Alternatively, would it also be slow if I had to use getElementById()
everytime I needed to access the node?


If you had to access a node many times, then getElementById method
would be slower than if you had stored the node.


ct******@gmail.com wrote:

Hi Everyone,

Is it faster to save the precise location of an html dom node into a
variable in js, or to use getElementById everytime you need to access
the node?

That depends on how often you intend to access the node. If it is less
than a few hundred times in a tight loop, it likely makes no noticeable
difference.

I''d suggest testing it to find out where the tipping point is, then
deciding if you are ever likely to approach it.

I want to make my application as fast as possible. I have about 10-20
id tags that need to be accessed and modified from time to time. Would
the jvm perform slowly if I stored all of the dom node strings
"document.node.child...." into a huge js array?

I don''t think an array of 20 items is huge. 20,000 is large.

Alternatively, would it also be slow if I had to use getElementById()
everytime I needed to access the node?

"Slow" is relative. If you are accessing the nodes infrequently, then
it likely makes zero difference to the perceived speed.

It is difficult to recommend a particular approach without knowing more
about the use to which it will be put, but generally I prefer to store
references in an array. I prefer to identify the nodes to store using
some strategy other than say a sequence of IDs (el-0, el-1, etc.), or a
list provided as an array from some other source.

To me it''s much easier to wrap the elements in question in a div and
pass its ID to a function, then use getElementsByTagName or similar to
find the ndoes I want (maybe further distinguished by CSS class name)
and store references to them. For some nodes you can use a common name
attribute and getElementsByName, but that only suits a small sub-set of
elements.
--
Rob


这篇关于什么更快,将HTML DOM节点保存为变量,还是使用getElementById?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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