Using places, in relation to jquery.html vs innerHTML, the second option proved considerably faster by about 29% over the first. This is a somewhat considerable difference, without taking into account Jquery’s loading time, unlike Innerhtml which is native to Javascript.
However, in that second test, the innerHTML was slower than the jquery.append and jquery.html.
But how so???
What happens here is the following, 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;
As early as Monday:
var list = document.getElementById('list');
for (var i = 0; i < len; i++) {
list.innerHTML = list.innerHTML + '<div>Test ' + i + '</div>';
}
That is, the two tests have similar purposes but in terms of performance we can notice a very distinct number, this is due to the access of the innerHTML in the second test be done within the for, may seem silly, but each time looping the same is accessed and this small slip can generate a catastrophic slowness.
Now one more thing about security...
According to the MDN: "[...]it is recommended that you do not use the innerHTML when inserting plain text as an alternative, use Node.textContent. This does not interpret the past content as HTML, but instead inserts it as a pure text."
This part concerns the implementation of <script>
and security failures in the use of innerHTML (for more details I suggest you see the link) and as an alternative it is suggested the use of Node.textContent.
The Node.textContent represents the node content of the element and its descendants and the main difference between it and the innerHTML is that the first does not analyze HTML as a whole, only the textNode making it more performatic in relation to the second.
There are several other methods as: innerText and nodeValue, I will not talk in detail of all so that the content is not too extensive more with a brief search you can see what each one does. Well, let’s get down to business, this test, includes all cited native methods tested on different elements.
But, which one to use? The textContent In this last test it was more performative in relation 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, both in terms of safety and performance is to use the same. Another point to take into account is the fact of being native to Javascript.
Regarding native selectors the selector getElementByID is faster than selectors like getElementsByClassName and querySelector according to this test. Id also wins in selector testing on Jquery in this test.
What to use? no first test and in that third you can clearly see that again native features come out in front of performance.
The first two tests were taken from Stackoverflow.
Here a list of other interesting topics regarding javascript performance, recommend as additional reading.
Obs.: the values can vary in thousandths depending on the machine and browser.
have you tried using a js library to do this ? http://i18next.com/
– Gabriel Rodrigues
@Gabrielrodrigues had no knowledge of this lib, very cool, but I want to develop my own, and I thought of something very simple, no need for attributes or something except the text.
– Lucas Fontes Gaspareto
you use some template engine or intend to write from scratch in pure js ?
– Gabriel Rodrigues
@Gabrielrodrigues intend to do in pure js.
– Lucas Fontes Gaspareto
For anyone who needs something like this server side, I have a PHP solution here: http://answall.com/a/97733/70
– Bacco