0

Lets say I have an element #parentDiv which I want to hide/show in various functions. Should I save this div to a variable and then reference this variable in my functions such as :

var myDiv = $(#parentDiv);

function hideDiv() { myDiv.hide(); }

function showDiv() { myDiv.show(); }

Or should I 'find' this div in both functions such as:

function hideDiv() { $("#parentDiv").hide(); }

function showDiv() { $("#parentDiv").show(); }

I've been reading up on scope lookup and was wondering performance-wise if it's faster for jquery to search for and element multiple times (across different functions), or if its faster to simply look up the scope.

hjalpmig
  • 702
  • 1
  • 13
  • 39
  • https://learn.jquery.com/using-jquery-core/selecting-elements/#saving-selections – adeneo Apr 13 '17 at 09:14
  • First case will be faster, but if you are not calling these functions thousands of times the difference will be negligible. – Ozan Apr 13 '17 at 09:26

0 Answers0