问题描述
当清除诸如选择框,表或列表之类的HTML元素时,删除节点是否更好/更快(例如, select.options.remove(i)
, table.deleteRow(i)
)或只是清空innerHTML(例如, select.innerHTML =
)?或者是否重要?
一个例子是重新初始化一个表。特定的选择字段的值应为后续的HTML表加载不同的值。当选择值发生变化时,表格需要重新初始化。
我做了一个没有坏的新测试。 / p>
由于部分样本设置是测试的一部分,因此这可能会使结果偏斜,这并不完美。 >
这使得 innerHTML
更快,但我不知道多少。
When clearing HTML elements such as select boxes, tables, or lists, is it better/faster to remove the nodes (e.g., select.options.remove(i)
, table.deleteRow(i)
) or just empty the innerHTML (e.g., select.innerHTML = ""
)? Or does it matter?
An example case would be reinitializing a table. A specific select field's value should load different values for a subsequent HTML table. When the select value changes, the table needs to be reinitialized.
I made a new test that isn't broken.
http://jsperf.com/innerhtml-vs-removechild/67
It's not perfect either since part of the sample setup is part of the test so this might skew the results.
This gives that innerHTML
is faster but I don't know by how much.
这篇关于删除子节点(或元素)或设置innerHTML =“”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!