Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Is there a performance enhancement to create dom elements native vs innerHTML?

Suppose I want to do this:

let el = document.getElementById("some_div");
el.innerHTML = "<div><h3>Hello There</h3><div>Hello World</div></div>";

Would it be better to write it as:

let outer_div = document.createElement("div");

let h3 = document.createElement("h3");

let inner_div = document.createElement("div");

let hello = document.createTextNode("Hello World");

inner_div.appendChild(hello);
outer_div.appendChild(h3);
outer_div.appendChild(inner_div);

Or does it not even matter coding wise in todays’ environments?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

I suggest to use createElement. It is faster and more secure. Imagine you load user input directly into your DOM with innerHTML. This can be a nasty security hole.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading