Performance Comparison Between createElement and innerHtml

Source: Internet
Author: User

Performance Comparison Between createElement and innerHtml

In js, there are roughly two ways to add html dynamically. One is to create a document. createElement method, and then add it using Element. appendChild or Element. innerHTML = sHTML method. The preceding two methods are obviously more flexible. Apart from the Element. appendChild method, there are other dom operations such as insertBefore. Flexibility is not discussed here, but performance is explored.

The comparison method is simple:

Generate 10000 pieces of html, such as performance test ......, and put them into the body at one time. record the whole process, time consumed, and run 10 times, and output time consumed.

The Code is as follows:

Window. onload = function () {testPerformance (10, function () {document. body. appendChild (generateHtmlElement (10000);}) testPerformance (10, function () {document. body. innerHTML = generateHtmlString (10000) ;})} function testPerformance (iTimes, fn) {var iLastTime = null; var iTotalTime = 0; for (var I = 1; I <= iTimes; ++ I) {iLastTime = new Date (). getTime (); fn (); var iCost = new Date (). getTime ()-iLastTime; console. log ("+ I +" Times: "+ iCost +" millisecond "); iTotalTime + = iCost; document. body. innerHTML = ""; iLastTime = new Date (). getTime ();} console. log ("Total time:" + iTotalTime + "millisecond"); console. log ("Average time:" + iTotalTime/iTimes + "millisecond");}/** use document. createElement and other dom methods to generate elements to test the performance .... * times: Number of generated div * return Element */function generateHtmlElement (iTimes) {var result = document. createElement ("div"); for (var I = 0; I

During String concatenation, a custom StringBuilder class is used to improve the String concatenation performance.

The test results are as follows:

In chrome, the difference is not too much, and innerHTML is slightly better:

In Firefox, innerHTML completes the createElement

IE11. It should be said that IE11 was cracked by chrome and Firefox.

To sum up, in terms of performance, innerHTML is faster than the createElement creation element in the append into the dom. in IE, the difference is farther. Why are you so slow, IE ?! Is there a problem with my testing method ?!

Related Article

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: 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.