tags:

views:

69

answers:

4

I currently have a single jQuery script page that I include in my ScriptManager on my MasterPage (I use ASP.NET). All of my custom scripts for all my pages go in this page. I know some people like to break it up into multiple pages but I prefer having them all in one place. Anyways, I have one $(document).ready(function(){ //do stuff// }); on this page. All of my other functions I name and then place in the document.ready function, ie:

$(document).ready(function(){ 
    Function1(); //necessary for page1.aspx, page2.aspx but not 3 or 4
    Function2(); //etc., necessary for some but not other pages
});

function Function1() {
    // whatever, etc //
}

My question centers around the efficiency of this. A lot of these functions only have any sort of application on certain pages, but not on all. Would it be better to include some page-specific coding, ie, determine what page we're on, and then if we're on the correct page, go ahead and perform the function, or does it not even matter? I was under the impression that it didn't matter, but I would like to be sure.

Thanks

+1  A: 

I can't say if this is a definitive answer, but where possibly in any project i try to display and show as little coding as possible that is not relevant to that page.

So in the case of JQuery, i would only display the javascript code required for that page by doing a server side check.

But i guess this would really depend on how large the code your using, if it's a small one line script in jquery then i don't think you're really going to lose out, but if you have a lot of logic in there, then i would recommend breaking it down per page that requires it.

Shadi Almosri
would a practical way of going about this be something like var path = window.location.pathname; and then parsing the path variable at the beginning of every function? or would i be better off breaking up into several .js files? also, if i did the latter, how would i add those on a per-page basis w/my script manager in the masterpage?
Jason
I'm not really sure how your script manager works or ASP, i am predominantly a PHP developer. Depending on the setup and number of files i would either use an if statement or a switch to work out the pages and then output the html to include the script. Not too much harm doing it client side either! There are many ways to really include your files i cant really say which is the best for you. At 200 lines (so far) i would say including it in every page isn't too much of an issue. (but then that really depends on the usage of the site - if it gets ALOT of users maybe you want to save bandwidth)
Shadi Almosri
+1  A: 

Depending on the number of page-specific functions, it could matter a lot.

If you have a small amount of extra data then you won't see any noticeable performance decreases. However, as your code base grows it could become a mess to maintain that code.

Additionally, you are pushing extra bits across the wire in every page request, as traffic increases that may add up to extra costs on your bandwidth.

AgileJon
+1  A: 

You might consider putting everything into an external .js file. Then the browser only has to download the code once.

If the functions cause errors on pages where they're not not needed, you should only include them on pages where they are needed.

Otherwise, it depends on how long it takes the functions to execute. If it's not noticeably long, I would say don't worry about it. It's not worth the fragility you'd introduce by adding a bunch of if statements.

Patrick McElhaney
i have it all in an external .js file (as mentioned in the question) that is loaded in the scriptmanager on the masterpage... but no errors are introduced by unnecessary functions, and the scripts are not that big (the file is under 200 lines right now)
Jason
Then I see no compelling reason to change what you have. Come to think of it, I have lots functions that may or may not do anything depending on the content of the page, and have for years.
Patrick McElhaney
+1  A: 

Yes. It matters. The real question is how complicated those functions are. If you think the code to check if the functions are needed would be more time consuming than running the code, then it's not necessary.

So that is the question you need to ask yourself (or test): Does it take longer to test if I need the function, or just to run them?

Jeff Davis