5311 posts The Dude Abides
  • United States
  • Exclusive Author
  • Has been a member for 5-6 years
  • Elite Author
  • Bought between 100 and 499 items
  • Referred between 100 and 199 users
  • Author had a Free File of the Month
+5 more
CodingJack says

Let’s say I have two elements where one is always visible and the other is always hidden. And their visibility swaps on multiple occasions. What is the fastest jQuery approach?

el.hide();
el.show();

or

el.detach(); // need to use detach instead of remove
el.appendTo(parent);

My instincts tell me that append/detach is a bigger performance hit than hide/show. But on the other hand, my guess is that hide() probably doesn’t release memory and detach() could. Any thoughts?

5762 posts
  • Exclusive Author
  • Author had a File in an Envato Bundle
  • Elite Author
  • Has been a member for 6-7 years
  • Sold between 100 000 and 250 000 dollars
  • Repeatedly Helped protect Envato Marketplaces against copyright violations
  • India
+3 more
VF says

Performance test says ‘detach’ is better than ‘hide’ for elements with text content. But not sure if this works same way for image / canvas and nested elements.

Generally detach is a best practice not just to show/hide but reduces dom operation in the situations when too many changes needed on element/children.

5311 posts The Dude Abides
  • United States
  • Exclusive Author
  • Has been a member for 5-6 years
  • Elite Author
  • Bought between 100 and 499 items
  • Referred between 100 and 199 users
  • Author had a Free File of the Month
+5 more
CodingJack says

Thanks dude :)

by
by
by
by
by
by