Title: Caching of Downloaded Code: Testing Results
Author: Alex Kirk
Published: October 11, 2005
Last modified: October 12, 2005

---

# Caching of Downloaded Code: Testing Results

October 11, 2005

Today 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](https://alex.kirk.at/2005/10/10/better-code-downloading-with-ajax/),
[XmlHttpRequest as a GET](https://alex.kirk.at/2005/10/05/code-downloading-with-ajax/)
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))

![Safari Code Downloading Cache Test](https://alex.kirk.at/wp-content/safari_cachetest.
png "Safari Code Downloading Cache Test")

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](https://alex.kirk.at/area7/2005/10/11/).
Please do so and correct my values if you receive different results.

The sources of my tests are available, too: [index.phps](https://alex.kirk.at/area7/2005/10/11/index.phps)
and [js.phps](https://alex.kirk.at/area7/2005/10/11/js.phps). I did my testings 
using the latest [prototype.js library](http://dev.conio.net/repos/prototype/dist/prototype.js).
Maybe I will try it later on with another library (e.g. with [dojo](http://www.dojotoolkit.org/).
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.

XmlHttpRequest, caching, prototype.js, test

[Code](https://alex.kirk.at/category/code/)

Read this next

[Better code downloading with AJAX](https://alex.kirk.at/2005/10/10/better-code-downloading-with-ajax/)

## 7 responses to “Caching of Downloaded Code: Testing Results”

 1.  ![Marc Brooks Avatar](https://secure.gravatar.com/avatar/589ead2e04c5d2e5a69de273cbb02aed6458a0ffda041fdc9e0885c9684a29e1?
     s=48&d=mm&r=g)
 2.  [Marc Brooks](http://musingmarc.blogspot.com)
 3.  [October 11, 2005](https://alex.kirk.at/2005/10/11/caching-of-downloaded-code-testing-results/comment-page-1/#comment-25)
 4.  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).
 5.  [Log in to Reply](https://alex.kirk.at/wp-login.php?redirect_to=https%3A%2F%2Falex.kirk.at%2F2005%2F10%2F11%2Fcaching-of-downloaded-code-testing-results%2F)
 6.  ![Michael Mahemoff Avatar](https://secure.gravatar.com/avatar/19bf57e57589a1d5207f96a85d592a8f40e502b88d8b13b057eca89e82264a58?
     s=48&d=mm&r=g)
 7.  [Michael Mahemoff](http://ajaxpatterns.org)
 8.  [October 12, 2005](https://alex.kirk.at/2005/10/11/caching-of-downloaded-code-testing-results/comment-page-1/#comment-26)
 9.  Alexander, have you tried setting headers to encourage caching (in the XHR call
     as well as the server output)?
 10. 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.
 11. [Log in to Reply](https://alex.kirk.at/wp-login.php?redirect_to=https%3A%2F%2Falex.kirk.at%2F2005%2F10%2F11%2Fcaching-of-downloaded-code-testing-results%2F)
 12. ![Alexander Kirk Avatar](https://alex.kirk.at/wp-content/uploads/sites/2/2025/
     06/cropped-2025.Alex-512x512-1-48x48.jpg)
 13. [Alexander Kirk](http://alex.kirk.at/)
 14. [October 12, 2005](https://alex.kirk.at/2005/10/11/caching-of-downloaded-code-testing-results/comment-page-1/#comment-27)
 15. I have used server side headers `Cache-Control` and `Last-Modified` (as can be
     seen in [js.phps](http://alex.kirk.at/area7/2005/10/11/js.phps)). The Browser 
     should use the `If-Modified-Since` header by it self.
 16. I’ve researched [a bit more](http://www.mnot.net/cache_docs/) and now explicitly
     use the `Expires` field. I have updated my post with the new results.
 17. [Log in to Reply](https://alex.kirk.at/wp-login.php?redirect_to=https%3A%2F%2Falex.kirk.at%2F2005%2F10%2F11%2Fcaching-of-downloaded-code-testing-results%2F)
 18. ![Erik Arvidsson Avatar](https://secure.gravatar.com/avatar/c9e45c66e3481626cec0d7e65e7ff0adeb8ec36abec2c51340b15e137c6693e4?
     s=48&d=mm&r=g)
 19. [Erik Arvidsson](http://erik.eae.net/)
 20. [October 12, 2005](https://alex.kirk.at/2005/10/11/caching-of-downloaded-code-testing-results/comment-page-1/#comment-31)
 21. Also see [http://me.eae.net/archive/2005/04/02/xml-http-performance-and-caching/](http://me.eae.net/archive/2005/04/02/xml-http-performance-and-caching/)
 22. [Log in to Reply](https://alex.kirk.at/wp-login.php?redirect_to=https%3A%2F%2Falex.kirk.at%2F2005%2F10%2F11%2Fcaching-of-downloaded-code-testing-results%2F)
 23. [Ajaxian](http://www.ajaxian.com/archives/2005/10/browser_caching.html)
 24. [October 13, 2005](https://alex.kirk.at/2005/10/11/caching-of-downloaded-code-testing-results/comment-page-1/#comment-33)
 25. **Browser Caching Behavior Compared**
 26. 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…
 27. [Log in to Reply](https://alex.kirk.at/wp-login.php?redirect_to=https%3A%2F%2Falex.kirk.at%2F2005%2F10%2F11%2Fcaching-of-downloaded-code-testing-results%2F)
 28. ![Cris Perdue Avatar](https://secure.gravatar.com/avatar/f50c057ad46208ad7e939529c92fbcf735a59b99950f146862662b43ca61b9cf?
     s=48&d=mm&r=g)
 29. [Cris Perdue](http://perdues.com/)
 30. [October 13, 2005](https://alex.kirk.at/2005/10/11/caching-of-downloaded-code-testing-results/comment-page-1/#comment-34)
 31. 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.
 32. [Log in to Reply](https://alex.kirk.at/wp-login.php?redirect_to=https%3A%2F%2Falex.kirk.at%2F2005%2F10%2F11%2Fcaching-of-downloaded-code-testing-results%2F)
 33. ![Aidan Black Avatar](https://secure.gravatar.com/avatar/8e2b50f9790edaf266dab0e15958c6a1a576c580543c3bf1a2a0856322d11703?
     s=48&d=mm&r=g)
 34. [Aidan Black](http://www.gatewayedi.com)
 35. [September 7, 2006](https://alex.kirk.at/2005/10/11/caching-of-downloaded-code-testing-results/comment-page-1/#comment-5061)
 36. 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.
 37. [Log in to Reply](https://alex.kirk.at/wp-login.php?redirect_to=https%3A%2F%2Falex.kirk.at%2F2005%2F10%2F11%2Fcaching-of-downloaded-code-testing-results%2F)

### Leave a Reply 󠀁[Cancel reply](https://alex.kirk.at/2005/10/11/caching-of-downloaded-code-testing-results/?output_format=md#respond)󠁿

Only people in [my network](https://alex.kirk.at/friends/) can comment.

This site uses Akismet to reduce spam. [Learn how your comment data is processed.](https://akismet.com/privacy/)