Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Making a Cache Key That Can Be Found Again

Making a Cache Key That Can Be Found Again

In this lesson, you port the desk’s price cache to JavaScript and give it a key it can find again. The desk prices a request in pence: the word count times the rate per word, a stand-in for real pricing work that lets you count the calculations.

In Python, @lru_cache(maxsize=None) caches price(). Quoting the 400-word museum guide at 12 pence a word twice prints 4800 twice, and the counter shows one calculation: the second call found the first call’s result.

JavaScript has no lru_cache, so you write the wrapper yourself. memoize creates a Map and returns an arrow function, a closure that keeps that Map between calls. Its rest parameter collects the arguments into an array, the nearest thing to Python’s tuple of arguments, and the first port uses that array as the key. If the Map has no entry for the key, the wrapper stores the calculation’s result, then returns the stored value.

In that wrapper, fn(...args) spreads the array back into the call, like *args in a Python call. Called twice with the same request, the port logs 4800 twice, but its counter logs 2.

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

00:00 In this lesson, you’ll port the desk’s price cache to JavaScript and give it a key it can find again. The desk prices a request in pence: word count times rate, a stand-in for real pricing work.

00:12 In Python, @lru_cache caches it. Two quotes for the 400-word museum guide at 12 pence both print 4800.

00:23 The counter shows one calculation: the second call found the first result.

00:30 JavaScript has no lru_cache, so you write the wrapper yourself. memoize creates a Map for the results. It returns an arrow function, a closure that keeps that Map between calls.

00:43 Its rest parameter collects the arguments into an array, the nearest thing to Python’s tuple of arguments, and that array becomes the key. If the Map has no entry for that key, the wrapper stores the result.

00:56 fn(...args) spreads the array back into the call, like *args in a Python call. Then it returns the stored value. Below, price wraps the calculation, with a counter per run.

01:12 Now call it twice with the same request. Both calls log 4800, as in Python. Python’s counter showed one. Recall the intro’s result. Two calculations. The second call missed the cache, and the calculation ran again.

01:33 Each call collects its arguments into a new array. The first call stored its result under its array; the second looks up a different array with the same two numbers.

01:43 What does === return for these same-looking arrays? Your turn: pause the video and predict.

01:55 False. They’re two objects. A Map matches an object key the same way, by identity, like Python’s is, so the cache now holds two entries.

02:07 Python’s lru_cache builds its key from the argument values: one entry, one hit. Here the tuple analogy stops: JavaScript has no tuple, and every array is an object with its own identity.

02:22 The fix is a key that’s a value. The wrapper names its two inputs, but an array of them would still be a new object each call. JSON.stringify() turns the pair into a string, and that string is the key.

02:37 Strings compare by their characters, so the same two numbers make the same key. Run it again: 4800 twice, one calculation, as in Python. The Map holds one entry.

02:52 Like Python’s in, has asks whether the key is stored. A saved zero is falsy, the reason once kept its own flag, but has still finds it: a zero-pence rate, quoted twice, is calculated one time.

03:09 The key holds both inputs, because both decide the price. Word counts and pence rates are whole numbers, zero or more, and for those, JSON.stringify() gives exactly one string per pair.

03:23 Other kinds of input need a different key. The price depends only on its two arguments, not on this or anything that changes, so a stored price stays right.

03:34 The counter only counts runs, and a cache hit skips it.

03:39 The Map lives as long as the wrapper that holds it. Python’s cache_clear() empties the cache, so the next quote calculates again: the count goes from 1 to 2.

03:50 This wrapper has no clear method, so if the desk changes its pricing, wrap the new function with memoizePrice again and use that wrapper instead: it starts empty, and the old one keeps its entries.

04:03 Like maxsize=None, this cache never evicts: each new pair adds an entry. Plain lru_cache stops at 128 entries; JavaScript has nothing like that built in, so for a real workload, set a deliberate limit or use a library.

04:22 So cache repeatable work under a primitive key from every input, because a rest array never matches an earlier one. Check it with has, and know how long the entries live.

04:34 Next up is the exercise Reuse a Quote Without Reusing the Wrong Price. There, you’ll make three new requests return the right prices with only two calculations, and reuse a zero result.

Become a Member to join the conversation.