{"id":154,"date":"2026-11-18T07:52:00","date_gmt":"2026-11-18T07:52:00","guid":{"rendered":"https:\/\/tothepowerof2.com\/blog\/?p=154"},"modified":"2026-11-15T08:33:42","modified_gmt":"2026-11-15T08:33:42","slug":"powers-of-two-cache-sizes-web-images","status":"publish","type":"post","link":"https:\/\/tothepowerof2.com\/blog\/powers-of-two-cache-sizes-web-images\/","title":{"rendered":"Powers of Two in Cache Sizes and Web Images"},"content":{"rendered":"<p><strong>TL;DR \/ Summary:<\/strong> 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.<\/p>\n<h2>Why powers of two keep appearing<\/h2>\n<p>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.<\/p>\n<p>That padding is invisible in the design file and very visible in the profiler.<\/p>\n<p>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.<\/p>\n<h2>Choosing the width ladder<\/h2>\n<p>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.<\/p>\n<p>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.<\/p>\n<h3>A practical ladder<\/h3>\n<ul>\n<li><strong>2048<\/strong> \u2014 full-width hero, high-density screens<\/li>\n<li><strong>1024<\/strong> \u2014 tablet, standard desktop<\/li>\n<li><strong>512<\/strong> \u2014 mobile, cards, thumbnails<\/li>\n<li><strong>256<\/strong> \u2014 lists, avatars, previews<\/li>\n<\/ul>\n<p>Four sizes, not twelve. Every additional size multiplies the files to generate, store, and invalidate.<\/p>\n<h2>Buffers and cache alignment<\/h2>\n<p>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.<\/p>\n<p>Aligning widths to block boundaries \u2014 256 pixels is a safe multiple \u2014 means fewer wasted buffers in an image proxy.<\/p>\n<blockquote><p>An image pipeline is mostly memory management wearing a design costume.<\/p><\/blockquote>\n<p>Check the proxy&#8217;s configured block size before choosing the ladder. Aligning to it costs nothing and saves a surprising amount on high-traffic sites.<\/p>\n<h2>Storage and cache keys<\/h2>\n<p>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.<\/p>\n<p>Fewer sizes make purging reliable. When an image changes, four keys are easy to invalidate; twenty are not.<\/p>\n<p>Name the files with the width in the filename. A predictable name makes the cache key obvious and makes accidental duplicates easy to spot.<\/p>\n<h2>Quality settings per size<\/h2>\n<p>Small images tolerate higher compression because the artefacts are smaller. Large hero images need more quality or the gradients band.<\/p>\n<p>Set quality per rung rather than globally: aggressive on the 256 thumbnails, moderate on the 2048 hero.<\/p>\n<p>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.<\/p>\n<h2>When powers of two do not apply<\/h2>\n<p>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.<\/p>\n<p>The principle is to avoid needless arbitrary sizes, not to obey a rule for its own sake.<\/p>\n<p>If a design requires 720 pixels, serve 720. Just do not also serve 718 and 724 because nobody set a policy.<\/p>\n<h2>Documenting the policy<\/h2>\n<p>Write the ladder in the repository, next to the image build configuration. A policy in someone&#8217;s memory becomes a suggestion within a month.<\/p>\n<p>Add a check that rejects widths outside the ladder at build time. Automated enforcement is the only kind that survives a deadline.<\/p>\n<p>For the exponent arithmetic behind the ladder, <a href=\"https:\/\/tothepowerof2.com\/\">the power of 2 calculator<\/a> makes the halving sequence quick to verify, which helps when explaining the policy to a designer who reasonably asks why not 1440.<\/p>\n<p>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.<\/p>\n<h2>Serving the right size without JavaScript<\/h2>\n<p>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.<\/p>\n<p>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.<\/p>\n<p>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.<\/p>\n<blockquote><p>The browser is a better judge of screen size than any user-agent string.<\/p><\/blockquote>\n<p>Keep the ladder small so the choice is meaningful. Four rungs produce clear decisions; twelve produce near-identical files.<\/p>\n<p>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.<\/p>\n<h2>Retina screens and the top rung<\/h2>\n<p>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.<\/p>\n<p>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.<\/p>\n<blockquote><p>One missing rung looks worse than four extra kilobytes.<\/p><\/blockquote>\n<p>Measure the real request with the network panel, not by guessing. The browser&#8217;s decision is the only one that matters.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 &#8230; <a title=\"Powers of Two in Cache Sizes and Web Images\" class=\"read-more\" href=\"https:\/\/tothepowerof2.com\/blog\/powers-of-two-cache-sizes-web-images\/\" aria-label=\"Read more about Powers of Two in Cache Sizes and Web Images\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":61,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[],"class_list":["post-154","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-math-practice-examples"],"_links":{"self":[{"href":"https:\/\/tothepowerof2.com\/blog\/wp-json\/wp\/v2\/posts\/154","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/tothepowerof2.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/tothepowerof2.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/tothepowerof2.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/tothepowerof2.com\/blog\/wp-json\/wp\/v2\/comments?post=154"}],"version-history":[{"count":1,"href":"https:\/\/tothepowerof2.com\/blog\/wp-json\/wp\/v2\/posts\/154\/revisions"}],"predecessor-version":[{"id":155,"href":"https:\/\/tothepowerof2.com\/blog\/wp-json\/wp\/v2\/posts\/154\/revisions\/155"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/tothepowerof2.com\/blog\/wp-json\/wp\/v2\/media\/61"}],"wp:attachment":[{"href":"https:\/\/tothepowerof2.com\/blog\/wp-json\/wp\/v2\/media?parent=154"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/tothepowerof2.com\/blog\/wp-json\/wp\/v2\/categories?post=154"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/tothepowerof2.com\/blog\/wp-json\/wp\/v2\/tags?post=154"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}