Currently I’m using the following code to append certain elements to the DOM:
$('<p/>', { html: data } ).appendTo('.columnLeft');
But because a lot of elements need to be appended I’m a bit worried about performance. Before I used { html: data } I’ve tried { text: data }. But because sometimes there’s html in the data-variable I’ve changed it to { html: data }. I don’t really have the tools to measure performance, so I was wondering if someone here knows what is faster?
Thanks for reading!
As commented below, I’ve made my own testcase you can see it here. The solution Adam gave is the fastest according to the test.
The fastest way to append HTML elements to the DOM using jQuery is to just append them as an HTML string like this:
That way jQuery doesn’t go through the process of having to create DOM elements before they are actually appended to the DOM.