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 ?
$('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"
Source (PDF)
If you must get the best performance, ditch jQuery and use native JavaScript.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With