views:

85

answers:

4

Just curious on this, is it possible to cache javascript? That is, to minimize client cpu needed to recalculate some logic each time I refresh the browser?

Take the google javascript map for example. When I emebed the map on my page, is there any cache mechanism that I can use on my page?

+2  A: 

The client can cache the .js file locally (prevent it from downloading) but the operation the said file performs is performed on every load.

As for Google Maps, it needs to perform its operations to display the map. Other then letting the client cache the .js file (thus saving the download), there is not much you can do.

The best you can do is limit the amount of processing the client needs to do, or if the result of your processing is scalar (Strings, numbers, array of), you can store it in a cookie for later use. DOM manipulation is done on every load.

Do all your heavy processing on the server when possible.

Andrew Moore
+3  A: 

JavaScript execution will occur for each page load based. One alternative, is to change how you call the JavaScript by first checking to see if the value has already been calculated before executing the calculation. To accomplish this, you need to store the calculated value in some form of state such as session, the URL as a query string parameter, or in a cookie. This would ensure that the first time the page loads, the value is calculated and stored. For each subsequent page load, the value would be pulled from state rather than re-calculated.

PortageMonkey
+1  A: 

You can't cache the result of compiling the Javascript, but you can avoid loading parts of your application logic until they're needed - that is, at the moment you need some logic, add a new <script> tag through the DOM for the functionality you need.

bdonlan
can provide example?
cometta
GWT has some support for stuff along these lines - see http://googlewebtoolkit.blogspot.com/2008/11/improving-performance-with-on-demand.html and http://groups.google.com/group/Google-Web-Toolkit-Contributors/browse_thread/thread/eb9c8cf046cbaaf2
bdonlan
A: 

I think you're talking about the images loaded by the javascript, from google's servers?

There is a huge grid of images for each detail level and it doesn't logistically make sense to cache these. After a few minutes of scrolling around in google maps, you'd have enough images to fill your hard drive several times over!

Some browsers don't handle javascript as well as others. Firefox is temporarily lagging behind, but both Google Chrome and Safari are extremely fast. Safari is worth a download because it's Development tools will show you exactly what is taking so long to happen.

Sneakyness