JavascriptDOM operation performance ----- create DOM

Source: Internet
Author: User
In many cases, we need to create a series of element nodes, just like the following operations: Java code varlistdocument. getElementById (& amp; #39; content & amp; #39;); for (vari0; I & lt; 10; I ++) {varitemdocument. createElement (& amp; #39; li.

 

In many cases, we need to create a series of element nodes, just like the following operations:

Java code

Var list = document. getElementById ('content ');

For (var I = 0; I <10; I ++ ){

Var item = document. createElement ('lil ');

List. appendChild (item );

Item. appendChild (document. createTextNode ('item' + I ));

}

 

Var list = document. getElementById ('content ');

For (var I = 0; I <10; I ++ ){

Var item = document. createElement ('lil ');

List. appendChild (item );

Item. appendChild (document. createTextNode ('item' + I ));

}

 

In fact, this operation efficiency is very low. We need to constantly update the DOM in the partition. The page should be updated every time a loop occurs, and the number of cycles may not be particularly obvious. However, if there are many operations, the efficiency is very problematic. Let's look at the following code:

Java code

Var list = document. getElementById ('content ');

Var fragment = document. createDocumentFragment ();

For (var I = 0; I <10; I ++ ){

Var item = document. createElement ('lil ');

Fragment. appendChild (item );

Item. appendChild (document. createTextNode ('item' + I ));

}

List. appendChild (fragment );

 

Var list = document. getElementById ('content ');

Var fragment = document. createDocumentFragment ();

For (var I = 0; I <10; I ++ ){

Var item = document. createElement ('lil ');

Fragment. appendChild (item );

Item. appendChild (document. createTextNode ('item' + I ));

}

List. appendChild (fragment );

 

Here we should first put all the element nodes in fragment, and then add them to the DOM at one time. Here we only perform one DOM operation, which improves the performance, it can also avoid page flashes caused by non-stop DOM operations, which makes me feel a bit confused about StringBuffer/StringBuilder in java.

In the face of the above problems, we usually have another method, that is, using the innerHTML method. Although this is not a standard usage of DOM, it is many people (including me) they all like usage very much:

Java code

Var createDatagrid = function (rows ){

Var html = [];

Html. push ('

Html. push (' );

For (var I = 0; I <rows. length; I ++ ){

Html. push ('

Html. push ('

Html. push ('

Html. push ('

Html. push ('

Html. push ('

Html. push ('

Html. push ('

Html. push ('

}

Html. push ('

Html. push ('

'); '); '); '); '); '); '); '); '); '); ');
'+ Rows [I]. user_name +''+ Rows [I]. type_id +''+ Rows [I]. send_time +''+ Rows [I]. gps_time) +''+ Rows [I]. speed +''+ Rows [I]. course +''+ Rows [I]. gps_status +'
');

Document. getElement. innerHTML = html. join ("");

}

 

Var createDatagrid = function (rows ){

Var html = [];

Html. push ('

Html. push (' );

For (var I = 0; I <rows. length; I ++ ){

Html. push ('

Html. push ('

Html. push ('

Html. push ('

Html. push ('

Html. push ('

Html. push ('

Html. push ('

Html. push ('

}

Html. push ('

Html. push ('

'); '); '); '); '); '); '); '); '); '); ');
'+ Rows [I]. user_name +''+ Rows [I]. type_id +''+ Rows [I]. send_time +''+ Rows [I]. gps_time) +''+ Rows [I]. speed +''+ Rows [I]. course +''+ Rows [I]. gps_status +'
');

Document. getElement. innerHTML = html. join ("");

}

 

The above efficiency is also very high, and the efficiency of the second method is similar, but this method is not based on javascript DOM calls, it is created through the HTML Parser using its internal DOM call, so it is efficient

 

From lovebeyond

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.