Using replaces , relative to jquery.html vs. innerHTML , the second option was considerably faster by about 29% over the former. This is a fairly considerable difference, regardless of the loading time of Jquery, unlike the InnerHTML which is native to Javascript .
However, in this second test , innerHTML was slower compared to jquery.append and jquery.html .
But how come?
What happens here is as follows, note that in the first example the code is written as follows:
var list = document.getElementById('list');
var html = '';
for (var i = 0; i < 1000; i++) {
html += '<div>Test ' + i + '</div>';
}
list.innerHTML = html;
Already on the second:
var list = document.getElementById('list');
for (var i = 0; i < len; i++) {
list.innerHTML = list.innerHTML + '<div>Test ' + i + '</div>';
}
In other words, the two tests have similar purposes, but in performance, you can notice a very different number, this is due to the innerHTML access in the second test being done inside for , it may seem silly, but with every looping the same is accessed and this little slip can generate catastrophic slowness.
Now one more thing regarding security ...
According to MDN : "[...] it is recommended that you do not use innerHTML when inserting plaintext as an alternative, use node.textContent . This does not interpret content passed as HTML , but instead, insert it as plain text. "
This excerpt concerns the execution of <script>
and security holes in the use of innerHTML (for more details I suggest you see the link) and alternatively, node.textContent .
The node.textContent represents the contents of the element node and its descendants, and the main difference between it and the innerHTML is that the first one does not parse HTML as a whole, just the textNode making it more performative over the second.
There are several other methods such as: innerText and nodeValue , I will not speak in detail of everyone so that the content is not too long with a brief search you can see what each one does. Well let's go to what matters, this test , includes all the native methods quoted tested on different elements.
But which one to use? The textContent in this last test was more performative compared to the others, totaling an average of approximately 15% in comparison to the other forms. I believe that the best way based on what was described above, in both safety and performance relation is to use the same. Another point to take into consideration and the fact of being Javascript .
Regarding native selectors, the getElementByID selector is faster than selectors like getElementsByClassName and querySelector this test . Id also gains in the Jquery in this test selector.
Which one to use? in first test and in this third you can clearly see that new native features come out ahead of performance.
The first two tests were taken from the StackOverflow .
Here is a list of other interesting topics in relation the performance in javascript, I recommend it as an additional reading.
Note: Values may vary in milliseconds depending on the machine and browser.