The first method is better than the second, i.e.
** Creating an HTML element is better than manipulating the DOM "
Reason: working with the DOM may cause the browser to switch.
For example: suppose you need to replace an element with a DOM that already exists.
Using approach:
Create a DOM element . You create an item. Add verified attributes to it and replace it. The item will be added at a time, and there will be only one recount of the document .
Manipulating the DOM . You need to add and remove attributes or elements one at a time. This may cause the browser to initiate payment of all elements and attributes that are being processed. This will require significant resources for rendering the document flow when manipulating elements .
Thus, creating a dom element is much smoother, since the browser does not have to display the document flow again.
* EDIT: * If you need to insert many elements, the best way is to create a fragment of the document . The document fragment is in memory, not in the DOM tree. Thus, adding elements to it DOES NOT cause distortions. As the docs say:
Since the document fragment is in memory, and is not part of the main DOM tree, adding child elements to it does not cause a page overflow (calculating the element position and geometry). Therefore, the use of document fragments often leads to increased productivity.
source share