Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript document.createElement or HTML tags

Tags:

I am confused on following style of writing code. I want to know which is the practical and efficient method to insert HTML tag in document.

Javascript:

document.getElementById('demoId').innerHTML='<div>hello world and <a>click me</a> also</div>'; 

OR

var itd=document.createElement('div'),     ita=document.createElement('a'),     idt=document.createTextNode('hello world');     iat=document.createTextNode('click me');     idn=document.createTextNode('also');      ita.appendChild(iat);     itd.appendChild(idt);     itd.appendChild(ita);     itd.appendChild(idn)      docuemtn.getElementById('demoId').appendChild(itd); 

Which is the fastest and best method?

like image 222
blue ghhgdtt Avatar asked Mar 03 '13 04:03

blue ghhgdtt


People also ask

Should I use innerHTML or createElement?

#1) createElement is more performant However, using the innerHTML causes the web browsers to reparse and recreate all DOM nodes inside the div element. Therefore, it is less efficient than creating a new element and appending to the div.

What is document createElement in JavaScript?

createElement() In an HTML document, the document. createElement() method creates the HTML element specified by tagName, or an HTMLUnknownElement if tagName isn't recognized.

What is the purpose of HTML DOM createElement () method?

The HTML DOM createElement() method is used for creating an HTML element dynamically using JavaScript. It takes the element name as the parameter and creates that element node. You need to use the appendChild() method to have the newly created element as part of DOM .


1 Answers

Well just think about what each of them are doing. The first one is taking the string, parsing it and then calling document.createElement, document.appendChild, etc. (or similar methods) based on the output from the parsed string. The second is reducing the work load on the browser as you're stating directly what you want it to do.

See a comparison on jsPerf

According to jsPerf option 1 is approximately 3 times slower than option 2.

On the topic of maintainability option 1 is far easier to write but call me old fashioned, I'd prefer to use option 2 as it just feels much safer.

Edit

After a few results started coming in, the differing results piqued my curiosity. I ran the test twice with each of my browsers installed, Here is a screenshot of the results from jsPerf after all the tests (operations/second, higher is better).

jsPerf results

So browsers seem to differ greatly in how they handle the two techniques. On average option 2 seems to be the faster due to Chrome and Safari's large gap favouring option 2. My conclusion is that we should just use whichever feels the most comfortable or fits best with your organisation's standards and not worry about which is more efficient.

The main thing this exercise has taught me is to not use IE10 despite the great things I've heard about it.

like image 115
Daniel Imms Avatar answered Sep 17 '22 18:09

Daniel Imms