Issue link: https://read.uberflip.com/i/1146286
56 | Chapter 8: Rule 8: Make JavaScript and CSS External Despite these results, using external files in the real world generally produces faster pages. This is due to a benefit of external files that is not captured by these exam- ples: the opportunity for the JavaScript and CSS files to be cached by the browser. HTML documents, at least those that contain dynamic content, are typically not configured to be cached. When this is the case (when the HTML documents are not cached), the inline JavaScript and CSS is downloaded every time the HTML docu- ment is requested. On the other hand, if the JavaScript and CSS are in external files cached by the browser, the size of the HTML document is reduced without increas- ing the number of HTTP requests. The key factor, then, is the frequency with which external JavaScript and CSS com- ponents are cached relative to the number of HTML documents requested. This factor, although difficult to quantify, can be gauged using the following metrics. Page Views The fewer page views per user, the stronger the argument for inlining JavaScript and CSS. Imagine that a typical user visits your site once per month. Between visits, it's likely that any external JavaScript and CSS files have been purged from the browser's cache, even if the components have a far future Expires header (see Chapter 3 for more information about using a far future Expires header). On the other hand, if a typical user has many page views, the browser is more likely to have external components (with a far future Expires header) in its cache. The ben- efit of serving JavaScript and CSS using external files grows along with the number of page views per user per month or page views per user per session. Empty Cache vs. Primed Cache Knowing the potential for users to cache external components is critical to compar- ing inlining versus external files. We measured this at Yahoo! and found that the number of unique users coming in at least once a day with a primed cache ranges from 40–60% depending on the Yahoo! property. * The same study revealed that the number of page views with a primed cache is 75–85%. Note that the first statistic measures "unique users" while the second measures "page views." The percentage of page views with a primed cache is higher than the percentage of unique users with a primed cache because many users perform multiple page views per session. Users may show up once during the day with an empty cache, but make several subse- quent page views with a primed cache. See more information about this research in Chapter 3. * Tenni Theurer, "Performance Research, Part 2: Browser Cache Usage – Exposed!", http://yuiblog.com/blog/ 2007/01/04/performance-research-part-2/.

