Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Load JS files faster inside CGI script

by kalyanrajsista (Scribe)
on Feb 24, 2010 at 11:47 UTC ( #825050=perlquestion: print w/ replies, xml ) Need Help??
kalyanrajsista has asked for the wisdom of the Perl Monks concerning the following question:

I'm trying to figure out a better way to load my JavaScript (.js) files inside a CGI to load faster. My .js file is around 7 MB and including them to load at last (when page footer is loaded). How can i make my

~ How to Load my JS file faster ~ Does Compressing or Minifying my js files helps me anything??

Comment on Load JS files faster inside CGI script
Download Code
Re: Load JS files faster inside CGI script
by Corion (Pope) on Feb 24, 2010 at 11:56 UTC

    I'm not really sure where your question is about Perl.

    Some general hints:

    1. Benchmark to find where the bottleneck is. You're sending 7MB over the wire, which is a lot of data. Maybe split up the Javascript into smaller files that don't need to be loaded every access.
    2. If you're generating the Javascript from Perl, look whether you can create the Javascript as static files, thus reducing the time it takes until the answer can be sent.
    3. Consider eliminating the Javascript alltogether and only deliver information when it is requested instead of all information at once. This is the part where Perl is of most help I guess.

      Corion,

      I do agree with all your suggestions. But, here is what exactly I'm doing

      ~ I've a 2 dropdown list where data in second list box is to be displa +yed without actually loading the page. ~ To achieve that task, I've created my JS file with data before hand +and loading during onChange() event ~ My JS file contains a simple function along with data ( I'll mention + in below step what exactly is the data) which returns Options based +on the selection of first dropdown list ~ Data in JS is actually Hash of Arrays which piled up aroud 7MB

      How should I proceed. Is there any way I can minimize any of the steps so that my JS file size cannot pile up

        That's easy. Don't send the whole data, but request the data from Javascript when the user makes the first selection. For example the jQuery framework makes it really easy to request data without reloading the complete page. On the Perl side, you have a script that returns the options for the current selection.

Re: Load JS files faster inside CGI script
by zentara (Archbishop) on Feb 24, 2010 at 13:59 UTC
    My .js file is around 7 MB

    Now I remember why I use noscript. Isn't 7 meg getting a bit excessive for code that is just supposed to do minimal browser-side checking?


    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku
Re: Load JS files faster inside CGI script
by Mr. Muskrat (Abbot) on Feb 24, 2010 at 18:38 UTC

    You really should follow the advice already given but I thought that I would share a few thoughts that came to mind when I read your post.

    Split your JavaScript out into libraries so that you can only load the scripts that you need instead of sending everything every time. The browser will cache the libraries if you let it.

    Use AJAX for page updates where it makes sense, like updating a selection box when a selection is made in another selection box.

    If you are working with data and not HTML or JS snippets, then store and transmit it as JSON (JavaScript Object Notation -- you send a data structure that can be converted into a JS object). Once you have your JS object, it can be used or transformed however you like.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://825050]
Approved by keszler
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (15)
As of 2014-07-22 12:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (112 votes), past polls