Caching of Downloaded Code: Testing Results
Tuesday, October 11th, 2005 at 18:41 +0000 (UTC) by Alexander KirkToday I did some experimenting with the caching of downloaded code (or On-Demand Javascript, whatever you want to call it).
I've set up a small testing suite that currently tests 3 different ways of downloading code: script-tag insertion via DOM, XmlHttpRequest as a GET and XHR as a POST.
These are my results for now:
| Method | IE6 | Firefox 1.07 | Firefox 1.5b2 | Safari 2.0 | Opera 8.5 |
|---|---|---|---|---|---|
| script_dom | cached | cached | cached | cached | cached |
| xhr_post | not cached | not cached | not cached | not cached | not cached |
| xhr_get | cached | not cached | cached | not cached | not cached |
(Results are the same for Win and OS X where both browsers are available (FF & Opera))

This gives an interesting picture: Firefox does not seem to cache any scripts, neither the ones loaded via DOM nor those loaded via XHR. Only IE loads an XHR GET request from cache.
I've got the script in my public testing area, so you can test it for your own browser. Please do so and correct my values if you receive different results.
The sources of my tests are available, too: index.phps and js.phps. I did my testings using the latest prototype.js library. Maybe I will try it later on with another library (e.g. with dojo.io.bind).
I'd be interested in more ways to download code (especially via document.write since I haven't been able to include this properly to my tests) and in your results for other browsers. Just leave a comment.
UPDATE: I have now included the Expires header field with the Javascript file. Now FireFox in both version caches the script with script_dom, in version 1.5b2 it also caches XHR with GET requests.
October 11th, 2005 at 19:47 +0000 (UTC)
I get consistent results for IE 6.0.2900 (cached, not, cached), and for grins tested inside RSS Bandit, where we also get (cached, not, cached).
October 12th, 2005 at 02:32 +0000 (UTC)
Alexander, have you tried setting headers to encourage caching (in the XHR call as well as the server output)?
Also, related to document-write, one thing about script tag insertion is you can insert the script in the head or the body. Who knows if some browsers handle those differently.
October 12th, 2005 at 11:26 +0000 (UTC)
I have used server side headers
Cache-ControlandLast-Modified(as can be seen in js.phps). The Browser should use theIf-Modified-Sinceheader by it self.I've researched a bit more and now explicitly use the
Expiresfield. I have updated my post with the new results.October 12th, 2005 at 19:15 +0000 (UTC)
Also see http://me.eae.net/archive/2005/04/02/xml-http-performance-and-caching/
October 13th, 2005 at 16:58 +0000 (UTC)
Browser Caching Behavior Compared
We've talked about the necessity of using HTTP headers to control caching of content retrieved by XMLHttpRequest on IE. Alexander Kirk takes the conversation a step further with a detailed analysis of: the caching of downloaded code (or On-Demand Java...
October 13th, 2005 at 23:11 +0000 (UTC)
To hopefully clarify one issue a bit further, while Internet Explorer does cache
responses to XMLHttpRequest, it might cache more than you want
it to. I find that a Last-modified date in the response is not enough to stop IE6 from
caching, but adding an Expires response header persuades it to re-fetch the page.
September 7th, 2006 at 23:35 +0000 (UTC)
FWIW, IE behavior also changes depending on whether you are using SSL (https). From my own tests, it would appear that SSL prevents it from caching the XmlHttpRequest GET method.