Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery html() vs empty().append() performance

I have created a test case at http://jsperf.com/jquery-html-vs-empty-append-test to compare the performance of $.html() with $.empty().append(). I wondered that .empty().append() is faster.

Can anyone explain this performance gap?

Thanks.

like image 827
Arvind Bhardwaj Avatar asked Aug 23 '13 04:08

Arvind Bhardwaj


People also ask

Is innerHTML faster?

EVERYTHING I know on the topic says that working with the DOM ranges around 200 times faster than innerHTML.

Is innerHTML slow?

The use of innerHTML very slow: The process of using innerHTML is much slower as its contents as slowly built, also already parsed contents and elements are also re-parsed which takes time.

What is the difference between innerHTML and append () JavaScript?

Answer : appendChild is used to insert new node in DOM. innerHTML is a property of DOM that allows to replace content of an element with different HTML, which automatically gets parsed into DOM nodes.

What is the difference between the append () and after () methods in jQuery?

append insert the parameter element inside the selector element's tag at the last index position, whereas the . after puts the parameter element after the matched element's tag.


1 Answers

In your code $.empty().append() was running faster because, your selector was wrong,

You should have used var $test = $("#test"); instead of var $test = $("test"); for comparision.

See the DEMO Here.

like image 83
Akhil Sekharan Avatar answered Oct 21 '22 12:10

Akhil Sekharan