Skip to content

24 ways to impress your friends

Vote up?

Joe Mottershaw

This has been a very good read, it’s made me more aware of how sluggish my jQuery code actually is compared to how it could be!

I have a question though, you mentioned caching, how does this fair with using $(this). For example, if you only called upon an element once in your script for a click detection, would you need to assign it to a variable first if that element was the same element also be altered? Some examples…

1st Example

$(”#div_id”).click(function() { $(this).hide(); });

2nd Example

var div_id = $(”#div_id”); { div_id.hide(); });

Apologies for the bad example…
But is there much of a difference in loading time between the two?

Is $(this) a sort of variation of caching as it’s already selected the element and you’re just calling it back by saying “this one, the one you just found, yeah, use that”, or is it having to wade through all the code again to find it?