Analysis of Document.createdocumentfragment () and JS efficiency

Source: Internet
Author: User

It is very helpful for iterating the DOM of the bulk Operations page! Take advantage of document fragmentation, then append once and use native JavaScript statements to manipulate

Document.createdocumentfragment () is to save the use of DOM. Each JavaScript operation on the DOM will change the page's monetization and refresh the entire page, consuming a lot of time. To solve this problem, you can create a document fragment, attach all of the new nodes to it, and then add the contents of the document fragment to the file at once.

This is a simple test page I wrote:<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "
"
<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 "/>
<title>document.createdocumentfragment () test page </title>
<body>
<script type= "Text/javascript" >
varD1 =NewDate ();
for(vari=0;i<1000;i++)
{
varOp=document.createelement ("P");
varOtext=document.createtextnode ("Test1");
Op.appendchild (Otext);
Document.body.appendChild (OP);
}
varD2 =NewDate ();
document.write ("Method One time:" + (D2.gettime ()-d1.gettime ()) + "<br/>");
//---+-----
varD3 =NewDate ();
varofrag=document.createdocumentfragment ();
for(vari=0;i<1000;i++)
{
varOp=document.createelement ("P");
varOtext=document.createtextnode ("Test2");
Op.appendchild (Otext);
Ofrag.appendchild (OP);
}
Document.body.appendChild (Ofrag);
//in this piece of code
varD4 =NewDate ();
document.write ("Method two time:" + (D4.gettime ()-d3.gettime ()) + "<br/>");
</script>
</body>

Once the node is added to the document.body (or subsequent node), the page immediately reflects the change. For the first small program, running is no problem, but the problem is that it calls 10 times document.body.appendChild (), each time to produce a page refresh, which will produce a lot of page fragmentation. The second small piece of code, Document.body.appendChild (), is called only once, which means that only one page refresh is needed, and the time is obviously less than the previous one.

I used three kinds of browsers to test the above test code, the results are as follows:

IE7:

Method one time: 140

Method two time: 125

Firefox:

Method one time: 66

Method two time: 43

Chrome:

Method one time: 35

Method two time: 25

The results are consistent with the theory.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.