Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's faster when using jQuery to check visibility, $.data() or $.is(':visible')?

Today while I was writing some code for two methods that shows and hides a menu, I made a small test to see the most efficient way to check the visibility of the menu.

The results varied from a browser to another, FF 4.0b12 is faster using $.data, but Chrome (webkit) and Opera is faster when using $.is(':visible').

I couldn't test on IE9 , because the browser kept locking on me! Here is the test case: http://jsperf.com/data-or-display/3

So, what's the most efficient way to check visibility WITH jQuery ?

like image 389
Maher4Ever Avatar asked Mar 01 '11 03:03

Maher4Ever


1 Answers

$('whatever').is(':visible') reads the best. I think that is what matters. Unless you need to check hundreds of elements a second, I would not waste my time.

"We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil"

Donald Knuth

Source (PDF)

If you must get the best performance, ditch jQuery and use native JavaScript.

like image 159
alex Avatar answered Oct 06 '22 00:10

alex