7

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.

3 Answers 3

4

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Bit selector is same in both cases.
But its returning an empty jquery Object.
0

Use the selector correctly in jquery,

  $('#test').html('Example');

will be faster obviously than

  $.empty().append();

But

  $('test')

will search the DOM for an element with tag name 'TEST'.

Comments

0

In this section you should use innerHTML. because this is native Javascript.

see the http://jsperf.com/jquery-append-vs-html-list-performance/27

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.