views:

330

answers:

8

Hello,

I think this may be a browser dependent question- Suppose I have 10 Javascript files and several HTML pages. Suppose HTML pageA needs only JS1.js and JS3.js, similarly HTML pageB needs JS4.js and JS1.js. I want to know what would be effect of including all the 10 javascript files in all HTML pages? Will it directly relate to the memory consumption by the browser?

I am facing this problem particularly with YUI javascript library. There are several components like datatable, event, container, calendar, dom-event etc., The order in which they are included also seems to matter a lot- For example the dom-event js should be included before the rest for it to work. So to avoid all this confusion, I thought of including all these js files in a header file that gets included in all HTML pages.

The thing that I am worried about is the memory bloat and performance problems that it may cause. Please provide your suggestions on the same..

Thanks, -Keshav

+1  A: 

External scripts delay the display of the following html until they have loaded and executed. The impact is much less after the first page load, since they're already cached, though browsers will occasionally check for new versions, which still carries a delay. I try to limit the number of scripts and move the script tags to the bottom of the page when possible. Users won't notice the script loading delay if the page has already fully displayed.

David
Yes, I agree on the load times because of lot of javascripts. But any idea what happens to the performance / memory consumption of the browser because so many js files are loaded into memory at once?
Keshav
It's not much, unless the script's doing something horrid that it probably shouldn't be doing. Think tens or (occasionally) hundreds of kilobytes on a system with gigabytes of ram.
David
The Memory conception and performance are really based on browser.
Madhu
A: 

Scriptaculous implements a nice way to handle js dependencies. Guess you could check it out and "re-implement" it. ;D

As for memory bloat and performance issues... as long as your JS doesn't leak a lot (YUI probably doesn't) memory won't be much of a problem, although it will make your pages load slower, especially if loaded in the header.

wtaniguchi
+1  A: 

if a given script does nothing, it will not affect the performance. Obviously the first page will load slowly, but the rest will not need to load all the scripts because they will be cached. So the next pages will load faster

Tips:

1) Load the script at the bottom of the page (just before the closing BODY tag).

2) Use a non-blocking way of loading the scripts. This is the one I'm using .

<script type="text/javascript">

function AttachScript(src) {
 var script = document.createElement("script");
 script.type = "text/javascript";
 document.getElementsByTagName("body")[0].appendChild(script);
 script.src = src;
}
AttachScript("/js/jquery.min.js");
AttachScript("/js/ndr.js");
AttachScript("/js/shadowbox.js");
AttachScript("/js/libraries/sizzle/sizzle.js");
AttachScript("/js/languages/shadowbox-es.js");
AttachScript("/js/players/shadowbox-img.js");
AttachScript("/js/adapters/shadowbox-jquery.js");

Can't find the source web page though :-(

The Disintegrator
+1  A: 

Memory Consumption:

Assuming the scripts are well written then memory consumption and performance issues should be nominal. Your biggest problem with including all scripts at once will be the latency in the user experience first time through, or if you make changes, because they will have to download all of them in one hit. I think you should only include the scripts you need per page, not all scripts at once.

You can assess the impact yourself using simple tools like task manager/processes in Windows to monitor memory/processor useage, or plugs ins like Firebug for FireFox.

You can also look into something called minification to help make your script files as small as possible.

Dependencies:

The order in which you include the scripts is important as some scripts may depend on functionality in other scripts. So if the code in one script attempts to run and it requires code in another script that has not been downloaded then it will fail. My advice would be to actually understand those dependancies in your scripts files rather than just downloading everything at once because it seems easier.

rism
+1  A: 

Use the YUI Configurator to help determine the required file includes and order, as well as how to use the Yahoo! CDN combo service to combine all YUI files into a single script tag.

http://developer.yahoo.com/yui/articles/hosting/

Luke
+1  A: 

External assets to the HTML page are typically cached by the browser. External assets are anything requested from the HTML such as images, CSS, JavaScript, and anything else. So if you load all 10 script files up front you are forcing a one time massive download hit to your user. After this one time the user does not need to download the scripts again unless the modify timestamp on the files change.

Your page will only use what it requires. If a particular page requests js4.js and js5.js then all the functions in those files will be loaded into the interpreter in the order in which they are first requested from the HTML and second by the order in which they are specified in each of those files. If there are any namespace conflicts what ever is loaded into the interpreter last wins. The interpreter will clear out the functions once the page is unloaded from the browser.

For efficiency I would suggest using a server-side inclusion process to read each of the js files and include the contents of each file into a new single js file. This will reduce the number of HTTP requests to the server and save your users an extreme amount of bandwidth resources with regard to HTTP headers and GET requests. Also, put the request of this new one script file directly prior to the closing body tag of your HTML. Downloading of scripts block parallel downloads in IE, so you want to load scripts at the lowest possible point in the page.

A: 

You can read on caching methods using PHP to pass on several javascript files as one big JS file which includes everything you need. For additional performance gains, you can make the browser cache the file locally in addition to sending it gzipped (if the browser has support for the encoding using something like ob_start("ob_gzhandler");). By using gzip encoding, you can severely reduce the filesize of the main JS file you're sending which includes all your JS code (since plain text compresses so well). I recently had to do this on my own website and it's worked like a charm for both JS and CSS files.

http://www.ejeliot.com/blog/72

Note that by following the instructions on that tutorial, your JS file will only be sent once and the browser on the client's machine will keep a local copy stored which will also improve performance of every visit thereafter.

Also, consider googling "Minify" which should be hosted on Google Code.

+1  A: 

Any script you load into your page, even once downloaded and cached must still be parsed before the rest of the page can load. So in that sense there is a memory penalty, and there's still a potential for something in the script to significantly delay rendering.

However, in the case of a conscientiously designed library such as YUI I would expect the parsing time to be minimised.

If you can load all your scripts in at the end of the page, that can vastly improve performance as the entire page can render before being blocked by javascript execution, and your site will feel a lot snappier.

I would suggest investigating the Firebug Net panel and the YSlow extension to get specific performance stats for your website.

James Wheare