Tools & Infrastructure

How Does Caching Work?

Why your browser loads sites instantly the second time — and how your computer remembers things so it doesn't have to ask twice.

Scroll to start

Your Computer's Short-Term Memory

Think about the last time you visited a website. It loaded fast, right? Now think about the very first time you visited it — it probably took a little longer. That's caching at work.

Caching is like keeping your most-used toys on your desk instead of running to the toy box every single time you want to play. Your computer, phone, and even websites all keep copies of things nearby so they don't have to fetch them all over again.

When a page loads for the first time, the browser downloads images, text, and other pieces. It saves some of those pieces in a cache — a small storage spot right on your device. The next time you visit, the browser says "I already have that!" and shows it instantly instead of downloading it again.

Faster Pages, Happier People

Every second a page takes to load, some people leave. That's why caching is a big deal. When a site loads instantly, people stick around, read more, and come back again. When it's slow, they click away.

But it's not just about speed. Caching also lightens the load on servers — the computers that deliver website files to you. If a million people visit a site and the same logo file gets downloaded a million times, that's expensive. With caching, the browser only downloads it once, then serves it from local memory forever.

💡 Key Insight

Most of the things that make a website feel slow aren't the big, important parts — they're the tiny files like icons and buttons that add up. Caching handles all of that automatically.

The Three-Step Cache Loop

Caching works in a simple three-step loop that happens behind the scenes every time your browser loads a page.

The Cache Check Loop
🔍
Browser Checks
Does it have this file already?
💾
Cache Hit
File found — use the saved copy
Instant Load
Page renders fast
repeat on next page load

If the file isn't in the cache yet (called a cache miss), the browser downloads it fresh, saves a copy, and moves on. Different things get cached for different lengths of time — images might last a year, but a social media feed might expire in minutes so you always see new posts.

There are actually several different caches working at once: your browser's internal cache, a CDN (a network of caches spread across the world), and even caches built into your computer's memory itself.

Caching in JavaScript

Here's a simple example showing how caching works in code. This function remembers the results of an expensive calculation so it doesn't have to run twice.

cache-example.js
// A simple cache using a JavaScript object
const cache = {};

function fetchData(key) {
  // If the data is already in cache, return it instantly
  if (cache[key]) {
    console.log("Found in cache!");
    return cache[key];
  }

  // Otherwise, pretend to fetch (this could be an API call)
  console.log("Fetching fresh data...");
  const result = "Data for: " + key;

  // Save it to cache for next time
  cache[key] = result;
  return result;
}

fetchData("user-profile");  // → "Fetching fresh data..."
fetchData("user-profile");  // → "Found in cache!"
fetchData("settings");     // → "Fetching fresh data..."

The first call to fetchData with any key runs the full logic. The second call with the same key just returns the saved result — no extra work needed.

Knowledge Check

Test what you learned with this quick quiz.

Quick Quiz — 3 Questions

Question 1
What is a cache?
Question 2
What happens on a cache miss?
Question 3
Why do websites set different cache expiration times for different types of files?
🏆

You crushed it!

Perfect score on this module.