The tea shop's service worker keeps recent API responses and tea photos in memory, and memory on a phone is short. Counting entries doesn't bound it: one photo can weigh as much as a thousand menu items. Write a class ByteBudgetCache: new ByteBudgetCache(maxBytes) makes an empty cache whose bodies add up to at most maxBytes bytes. maxBytes must be a whole number of 1 or more; anything else is a RangeError.
set(key, body) stores body under key and returns true. A body is a string, an ArrayBuffer, a typed array, or a Blob (anything else is a TypeError), and its size is the bytes it takes: a string's length in UTF-8 ("Thé vert" is 9 bytes, though its length is 8), a buffer's or typed array's byteLength, a Blob's size. Setting a key that's already there replaces its body, and frees the old body's bytes before anything else happens. When the new body doesn't fit, evict entries, least recently used first, until it does, and no more: a cache filled exactly to its budget is fine.
A body larger than maxBytes can never fit: don't store it, evict nothing for it, and return false. Remove any entry already under that key, though, since its body is out of date.
get(key) returns the body, or undefined, and counts as a use; storing a body counts as a use too. has(key) says whether the key is there, without counting as a use. delete(key) removes an entry and returns whether there was one. keys() returns the keys from least to most recently used, size is the number of entries, and bytes is the total size of the bodies stored.
The JavaScript problems LRU cache (lru-cache) and TTL cache (ttl-cache) bound a cache by its number of entries and by time. This one is bounded by bytes, as a browser's own caches are, so one large entry can push out many small ones.
const photos = new ByteBudgetCache(1000);
photos.set("/teas/green.jpg", new Uint8Array(400));
photos.set("/teas/mint.jpg", new Uint8Array(400));
photos.get("/teas/green.jpg"); // green is now the most recently used
photos.set("/teas/oolong.jpg", new Uint8Array(300)); // 1100 bytes won't fit: mint goes
photos.keys(); // ["/teas/green.jpg", "/teas/oolong.jpg"]
photos.bytes; // 700
photos.set("/teas/banner.jpg", new Uint8Array(1200)); // false: bigger than the whole budget
const cache = new ByteBudgetCache(1000);
expect(cache.set("/api/menu", '{"teas":3}')).toBe(true);
cache.set("/teas/green.jpg", new Uint8Array(400));
expect(cache.get("/api/menu")).toBe('{"teas":3}');
expect(cache.get("/teas/rooibos.jpg")).toBe(undefined);
expect(cache.size).toBe(2);
expect(cache.bytes).toBe(410);
const cache = new ByteBudgetCache(1000);
cache.set("name", "Thé vert");
expect(cache.bytes).toBe(9);
cache.set("cup", "🍵");
expect(cache.bytes).toBe(13);
cache.set("buffer", new ArrayBuffer(100));
cache.set("floats", new Float64Array(10));
cache.set("photo", new Blob(["a".repeat(50)]));
expect(cache.bytes).toBe(243);
const cache = new ByteBudgetCache(1000);
cache.set("green", new Uint8Array(300));
cache.set("mint", new Uint8Array(300));
cache.set("oolong", new Uint8Array(300));
cache.set("banner", new Uint8Array(500));
expect(cache.keys()).toEqual(["oolong", "banner"]);
expect(cache.bytes).toBe(800);
expect(cache.has("green")).toBe(false);
const cache = new ByteBudgetCache(600);
cache.set("green", new Uint8Array(200));
cache.set("mint", new Uint8Array(200));
cache.set("oolong", new Uint8Array(200));
cache.get("green");
expect(cache.has("mint")).toBe(true);
cache.set("black", new Uint8Array(200));
expect(cache.keys()).toEqual(["oolong", "green", "black"]);
expect(cache.has("mint")).toBe(false);
const cache = new ByteBudgetCache(500);
cache.set("menu", new Uint8Array(250));
cache.set("reviews", new Uint8Array(250));
expect(cache.keys()).toEqual(["menu", "reviews"]);
expect(cache.bytes).toBe(500);
const cache = new ByteBudgetCache(1000);
cache.set("menu", new Uint8Array(400));
cache.set("reviews", new Uint8Array(400));
expect(cache.set("menu", new Uint8Array(600))).toBe(true);
expect(cache.keys()).toEqual(["reviews", "menu"]);
expect(cache.bytes).toBe(1000);
expect(cache.get("menu").byteLength).toBe(600);
const cache = new ByteBudgetCache(1000);
cache.set("menu", '{"teas":3}');
cache.set("banner", new Uint8Array(300));
expect(cache.set("banner", new Uint8Array(1001))).toBe(false);
expect(cache.has("banner")).toBe(false);
expect(cache.keys()).toEqual(["menu"]);
expect(cache.bytes).toBe(10);
const cache = new ByteBudgetCache(100);
cache.set("menu", "x".repeat(40));
expect(cache.delete("menu")).toBe(true);
expect(cache.delete("menu")).toBe(false);
expect(cache.bytes).toBe(0);
expect(cache.size).toBe(0);
expect(() => cache.set("count", 42)).toThrow(TypeError);
expect(() => new ByteBudgetCache(0)).toThrow(RangeError);
expect(() => new ByteBudgetCache(1.5)).toThrow(RangeError);
expect(() => new ByteBudgetCache("1000")).toThrow(RangeError);
Run tests runs every test on your code, here in your browser. The problem is solved when they all pass.
Ctrl + Enter runs the tests (⌘ + Enter on a Mac).
I'm here to help with A cache with a byte budget. I give hints, never the solution, so you get to solve it yourself. Ask for a hint, select code to ask about it, or press Ask Tuto why on a failing test.
Your first 3 answers need no account. for free to keep asking.