Powers of Two in Cache Sizes and Web Images

TL;DR / Summary: Image pipelines work best when sizes and buffers land on powers of two. Nine widths instead of forty, aligned to 256-pixel steps, cut storage and cache misses without a visible quality change.

Why powers of two keep appearing

Memory allocation, texture compression, and cache lines all operate in binary units. A texture whose width is not a power of two may be padded internally, copied, or decoded on the CPU instead of the GPU.

That padding is invisible in the design file and very visible in the profiler.

Web images are not always textures, but the same logic applies to thumbnail caches, sprite atlases, and CDN shard counts. Rounding to a power of two avoids a class of performance surprises.

Choosing the width ladder

Start from the largest size you actually serve and halve it until you reach the smallest. That produces a ladder where every step is a clean factor of two.

For a hero image at 2048 pixels, the ladder is 2048, 1024, 512, 256. Four files cover every real device, and each is a predictable step down.

A practical ladder

  • 2048 — full-width hero, high-density screens
  • 1024 — tablet, standard desktop
  • 512 — mobile, cards, thumbnails
  • 256 — lists, avatars, previews

Four sizes, not twelve. Every additional size multiplies the files to generate, store, and invalidate.

Buffers and cache alignment

Decoders allocate buffers in blocks. A 300-pixel-wide image still occupies a buffer sized for the next block up, so the memory saving is smaller than the pixel count suggests.

Aligning widths to block boundaries — 256 pixels is a safe multiple — means fewer wasted buffers in an image proxy.

An image pipeline is mostly memory management wearing a design costume.

Check the proxy’s configured block size before choosing the ladder. Aligning to it costs nothing and saves a surprising amount on high-traffic sites.

Storage and cache keys

Each size is a distinct cache key. Ten sizes means ten entries per image, ten invalidation paths, and ten chances for one to go stale.

Fewer sizes make purging reliable. When an image changes, four keys are easy to invalidate; twenty are not.

Name the files with the width in the filename. A predictable name makes the cache key obvious and makes accidental duplicates easy to spot.

Quality settings per size

Small images tolerate higher compression because the artefacts are smaller. Large hero images need more quality or the gradients band.

Set quality per rung rather than globally: aggressive on the 256 thumbnails, moderate on the 2048 hero.

Test one photographic image with a gradient and one with fine text. Those two cases expose most compression problems, and they behave differently at each size.

When powers of two do not apply

Art-directed crops, full-bleed backgrounds at unusual aspect ratios, and inline SVG have their own rules. Do not force an arbitrary power of two onto a design that needs an exact ratio.

The principle is to avoid needless arbitrary sizes, not to obey a rule for its own sake.

If a design requires 720 pixels, serve 720. Just do not also serve 718 and 724 because nobody set a policy.

Documenting the policy

Write the ladder in the repository, next to the image build configuration. A policy in someone’s memory becomes a suggestion within a month.

Add a check that rejects widths outside the ladder at build time. Automated enforcement is the only kind that survives a deadline.

For the exponent arithmetic behind the ladder, the power of 2 calculator makes the halving sequence quick to verify, which helps when explaining the policy to a designer who reasonably asks why not 1440.

The answer is not that 1440 is bad. It is that every extra size costs generation, storage, and invalidation, and four well-chosen rungs cover the devices you actually serve.

Serving the right size without JavaScript

Responsive image markup lets the browser choose a size from the ladder you published. The browser knows the viewport and the pixel density; your server does not.

Provide a width descriptor for each source and a sensible sizes attribute. Without the sizes hint, the browser assumes full width and picks a larger file than needed.

Test with the network panel open and a throttled connection. The chosen file should match the device, and a mismatch means the sizes hint is wrong.

The browser is a better judge of screen size than any user-agent string.

Keep the ladder small so the choice is meaningful. Four rungs produce clear decisions; twelve produce near-identical files.

Check the largest rung is actually used on high-density screens. A ladder that stops too low looks sharp in review and soft on a phone.

Retina screens and the top rung

A 2x display requests twice the CSS pixel width. A 400-pixel card on a retina phone needs an 800-pixel file, which is a rung most ladders forget.

Check the largest file actually served on a common device. If the browser is upscaling, add one rung rather than raising quality on all of them.

One missing rung looks worse than four extra kilobytes.

Measure the real request with the network panel, not by guessing. The browser’s decision is the only one that matters.