Is there a way that has better performance than the other when appending a new element to the dom with jQuery?
This is the way I usually do it:
$('<div class="foo">Hello World!</div>').appendTo($("#bar"));
But I’ve seen this quite a few times around :
$('<div/>').attr("class", "foo").text("Hello World!").appendTo($("#bar"));
I think that the first one is easier to read. Second one takes advantage of chaining but it in the end, the result is the same. But is it faster to do it one way of the other?
As you can see here: http://www.jsfiddle.net/SvCTK/
the difference in performance is pretty trivial (maybe not that trivial, find out for yourself). Source for benchmark:
You need a open
FireBugorWebkit developers toolsto see the results.