Command Palette

Search for a command to run...

Hectal
Back to the lesson: Topic 15.3 — Performance
JavaScript · Example 1 of 3

Memoisation: 21,891 calls down to 21

The second fastFib(20) made no new calls: the answer came straight from the cache. The recursive calls go through the memoised fastFib, so every sub-answer is cached too.

Fast JavaScript comes from doing less work, not from clever tricks: measure first with performance.now, marks and the DevTools Performance panel, then remove repeated work with memoisation, better data structures and batched DOM updates.

Change the code and press Run (Ctrl+Enter). Predict what will happen first, then break it on purpose and read what changes. Your edits are saved and match the lesson page.

Ready for more? This phase has 6 hands-on exercises with automatic checks.

Practice questions

Write the code in the editor, run it, then open the model answer to compare.

01

Write memoize(fn) that supports any number of arguments by building a key with JSON.stringify(args). Show that a counting function runs once for repeated calls.

02

You have orders (10,000 items with customerId) and vipIds (an array of 2,000 ids). Write the fast way to get the orders from VIP customers.

Explain it without notes

01

How would you find out why a page feels slow when typing in a search box?

02

What is memoisation, when is it safe, and what does it cost?

03

What is layout thrashing and how do you avoid it?

04

Why is performance.now() better than Date.now() for measuring code?

Memoisation: 21,891 calls down to 21

Preview

Press Run to see the page.

Console

Nothing logged yet.

Expected console output

6765 calls: 21891
6765 calls: 21
6765 calls: 21
5