DOM DocumentFragments

本文探讨了在JavaScript中使用DOMDocument碎片来优化DOM插入操作的性能提升。通过将DOM节点放入碎片中,然后一次性将碎片插入文档,可以显著减少DOM操作的时间消耗,尤其是在旧版浏览器中。此外,DOMDocument碎片还支持cloneNode方法,进一步提高了DOM插入代码的优化潜力。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

I was playing around with DOM DocumentFragments recently, in JavaScript, seeing what I could make with them. Roughly speaking, a DocumentFragment is a lightweight container that can hold DOM nodes. It’s part of the DOM 1 specification and is supported in all modern browsers (it was added to Internet Explorer in version 6).

In reading up on them I came across an interesting point, from the specification:

Furthermore, various operations — such as inserting nodes as children of another Node — may take DocumentFragment objects as arguments; this results in all the child nodes of the DocumentFragment being moved to the child list of this node.

This means that if you take a bunch of DOM nodes and append them to a fragment then you can simply append the fragment to the document, instead (and achieve the same result – as if you had appended each node individually). I instantly smelled a possible performance improvement here. I investigated a little bit further and noticed that DocumentFragments support the cloneNode method, as well. This provides all the functionality that you need to highly-optimize your DOM insertion code.

I set up a simple demo to test the theory.

Let’s take the situation where you have a bunch of DOM nodes that you need to append into the document (in the demo it’s 12 nodes – 8 at the top level – against a whole mess of divs).

  1. var elems = [
  2.     document.createElement("hr"),
  3.     text( document.createElement("b"), "Links:" ),
  4.     document.createTextNode(" "),
  5.     text( document.createElement("a"), "Link A" ),
  6.     document.createTextNode(" | "),
  7.     text( document.createElement("a"), "Link B" ),
  8.     document.createTextNode(" | "),
  9.     text( document.createElement("a"), "Link C" )
  10. ];
  11.  
  12. function text(node, txt){
  13.     node.appendChild( document.createTextNode(txt) );
  14.     return node;
  15. }

Normal Append

If we wanted to append these nodes into the document we would probably do it in this traditional manner: Looping through the nodes and cloning them individually (so that we can continue to append them all throughout the document).

  1. var div = document.getElementsByTagName("div");
  2.  
  3. for ( var i = 0; i < div.length; i++ ) {
  4.     for ( var e = 0; e < elems.length; e++ ) {
  5.         div[i].appendChild( elems[e].cloneNode(true) );
  6.     }
  7. }

DocumentFragment Append

However, when we bring DocumentFragments into the picture we can immediately see a different structure. To start we append all our nodes into the fragment itself (built using the createDocumentFragment method).

But the interesting point comes when it’s time to actually insert the nodes into the document: We only have to call appendChild and cloneNode once for all the nodes!

  1. var div = document.getElementsByTagName("div");
  2.  
  3. var fragment = document.createDocumentFragment();
  4. for ( var e = 0; e < elems.length; e++ ) {
  5.     fragment.appendChild( elems[e] );
  6. }
  7.  
  8. for ( var i = 0; i < div.length; i++ ) {
  9.     div[i].appendChild( fragment.cloneNode(true) );
  10. }

Setting some time stamps we can see our results pay off in spades:

BrowserNormal (ms)Fragment (ms)
Firefox 3.0.19047
Safari 3.1.215644
Opera 9.5120895
IE 6401140
IE 723061
IE 8b112040

As it turns out: A method that is largely ignored in modern web development can provide some serious (2-3x) performance improvements to your DOM manipulation.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值