Grokking the Modern Frontend System Design Interview
Vote

0% completed

​

Estimating Bytes Frames and Requests

The Numbers to Start From

Page Weight

Requests and Round Trips

The Frame Budget

Memory for a Long List

Updates per Second

The Estimates in One Table

Using This in an Interview

Key Takeaways

Practice Questions

Estimating Bytes Frames and Requests
Estimating Bytes Frames and Requests

An airline allows 23 kg in a checked bag. A traveler who weighs each item at home knows before leaving whether the bag will pass. A traveler who does not weigh anything finds out at the check-in desk. By then the only choices left are to pay or to throw things away.

A web page has limits of the same kind. The network moves a fixed number of bytes per second, every trip to a server costs a fixed time, and a 60 Hz screen asks for a new picture every 16.7 ms. A design can be weighed against these limits before any code exists, with arithmetic that fits on one page.

How much does Basket's product list weigh, how many trips does it need, and how much work fits into one frame? The sections below answer each question with a formula and a worked number.

The Numbers to Start From

Every estimate here uses the slow mobile profile from Frontend System Design vs Backend System Design: a 150 ms round trip and about 200 KB per second of download speed. The phone's processor is taken to be 4 times slower than a desktop's, as in Lighthouse's mobile test.

Every size, count and time below that is not part of that profile is an assumption, chosen as a reasonable value for a page like Basket's. The method is the point. When real sizes are known, the same formulas give real answers.

Page Weight

Page weight is the total number of bytes a page downloads, counted after compression, because compressed bytes are what travel over the network. The time to move them is the weight divided by the download speed.

Suppose Basket's product list downloads the following files:

FileAssumed sizeTime at 200 KB/s
HTML20 KB0.10 s
CSS50 KB0.25 s
JavaScript400 KB2.00 s
Two font files2 x 25 KB = 50 KB0.25 s
Images for the first 8 cards8 x 15 KB = 120 KB0.60 s
Images for the other 22 cards22 x 15 KB = 330 KB1.65 s
Total970 KB4.85 s
Page weight on the slow profile
Page weight on the slow profile

Look at the two largest pieces: the JavaScript and the 22 images the shopper cannot see yet. Together they are 730 KB of the 970 KB. If the lower 22 images load only when the shopper scrolls toward them, the first view needs 640 KB, which is 3.2 seconds of transfer instead of 4.85.

Notice also what the bar does not show: files that download at the same time still share the same 200 KB per second. Splitting 400 KB of JavaScript into ten files of 40 KB does not make the transfer faster. Total bytes divided by download speed is the shortest possible transfer time, however the bytes are split.

Requests and Round Trips

Bytes are one cost, and waiting is the other. A request cannot start until the page knows it needs it, and the page often learns that only from an earlier response.

The critical chain is the longest sequence of requests in which each one waits for the previous one. Each step in it costs at least one round trip. For Basket's product list, the chain is:

  1. Open a connection to Basket: 3 round trips, as in the earlier lesson's table.
  2. Request the HTML: 1 round trip.
  3. Request the CSS and JavaScript that the HTML names: 1 round trip, because the browser asks for both at once.
  4. The JavaScript requests the products: 1 round trip plus 80 ms of server work.
  5. Request the images that the product data names, from the CDN's own address: a new connection costs 3 round trips, plus 1 for the request.

That chain is 3 + 1 + 1 + 1 + 4 = 10 round trips, or 1,500 ms of pure waiting before a single byte of content is counted. A useful estimate for a page is therefore:

time = (round trips in the critical chain x 150 ms) + (bytes on that chain / 200 KB per second)

The number of steps in the chain matters more than the number of requests. Over HTTP/2 and HTTP/3, the browser sends many requests over one connection at the same time, so 30 images requested together cost about one round trip. Three API calls that each wait for the one before cost three.

The Frame Budget

The screen shows a new picture, called a frame, many times each second. The number of frames per second is the screen's refresh rate, measured in hertz (Hz).

The frame budget is the time the browser has to produce each frame: 1,000 ms divided by the refresh rate. At 60 Hz that is 16.7 ms, and at 120 Hz it is 8.3 ms. The page's JavaScript shares that time with the browser's own work: calculating styles, laying out the page and drawing the result.

Suppose the browser's own work takes about 6 ms per frame on the phone. At 60 Hz the page's code then has about 10.7 ms per frame, and at 120 Hz only about 2.3 ms.

The frame budget at 60 Hz and 120 Hz
The frame budget at 60 Hz and 120 Hz

Look at the 12 ms bar. A scroll handler that takes 3 ms on a desktop takes about 12 ms on a phone 4 times slower, and that is more than the 10.7 ms left at 60 Hz. The browser misses the frame, and the shopper sees the scrolling stutter.

A long task of 50 ms covers about three frames at 60 Hz (50 / 16.7) and six at 120 Hz. Anything that runs while the shopper scrolls or types has to fit inside a frame, or be split into pieces that do.

Memory for a Long List

The cost of a list grows with rows times nodes per row. A DOM node is one element or piece of text in the page's structure, and the browser keeps every node in memory and in its layout work.

Suppose a product card has 20 nodes. They include a wrapper, a link, an image, the name, the price, a badge, and the buttons with their labels. Thirty cards are 600 nodes. All 4,000 products in one long page would be 4,000 x 20 = 80,000 nodes, and a change that moves content near the top could make the browser lay out all of them again.

Images cost more once they are on screen. The browser decodes each image into raw pixels at 4 bytes per pixel. A card image shown at 160 by 160 CSS pixels on a phone with 2 screen pixels per CSS pixel needs 320 x 320 x 4 = 409,600 bytes, about 400 KB, although the file was 15 KB. Thirty such images take about 12 MB. Browsers may drop decoded images that are far off screen, but they cannot drop the nodes, so a list that keeps adding rows keeps growing.

Updates per Second

A live screen receives messages while it is open, and each message costs main thread time to handle and draw. The share of the main thread a live feature uses is updates per second times the cost of one update.

Basket's order tracking page receives the courier's position every 5 seconds, which is 0.2 updates per second. Even at 10 ms of work per update on the phone, that is 2 ms out of every 1,000, or 0.2 percent.

Compare a feature that shows live stock counts on the product list. Suppose the store sends every stock change for the whole catalog, about 2,000 a minute, which is 33 per second. If each one redraws the list in 8 ms on the phone, the main thread spends 33 x 8 = 264 ms of every second on stock counts, about 26 percent. Sending only changes for the 30 visible products, or drawing collected changes once per second, brings that close to zero.

The Estimates in One Table

QuantityHow to estimate itBasket's product listWhat it decides
Page weightSum of compressed bytes / download speed970 KB, 4.85 s; 640 KB, 3.2 s for the first viewWhat loads first and what waits
Critical chainDependent steps x round trip time10 round trips, 1,500 msWhich requests to start earlier or remove
Frame budget1,000 ms / refresh rate, minus browser work10.7 ms at 60 Hz, 2.3 ms at 120 HzHow much code may run during scrolling and typing
List memoryRows x nodes per row; pixels x 4 bytes per image600 nodes and 12 MB of images for 30 cardsHow many rows exist at once
Live updatesUpdates per second x cost per update0.2 per second for tracking; 33 per second for full stockWhat the server sends, how often the page draws

Using This in an Interview

Do one estimate out loud as soon as the main screen is clear: "At 200 KB per second, 400 KB of JavaScript is 2 seconds, so the first products must not wait for the whole bundle." One number like this justifies a design choice better than a list of techniques.

Two follow-up questions are likely:

  • "How many round trips are on your critical chain, and which one would you remove?"
  • "What happens to memory if the shopper scrolls through 2,000 products?"

Key Takeaways

  • State every size and count as an assumption, then show the arithmetic, so that real numbers can replace them later.
  • Transfer time is compressed bytes divided by download speed, and splitting files does not change the total.
  • Waiting time is the number of dependent steps in the critical chain times the round trip, so the chain's length matters more than the request count.
  • The frame budget is 16.7 ms at 60 Hz and 8.3 ms at 120 Hz, shared with the browser's own work, and a phone runs the same code several times slower.
  • List cost grows with rows times nodes, and each decoded image takes width x height x 4 bytes, whatever its file size.
  • A live feature's share of the main thread is its updates per second times the cost of each one.

These estimates show where a design will miss its limits before it is built. The next lesson, A Method for Any Frontend Design, puts requirements, estimates and the layers of the client into five steps that work for any product.

Practice Questions

Try each question first, then open the answer.

1. Basket's product page downloads 30 KB of HTML, 60 KB of CSS, 300 KB of JavaScript and one 90 KB main image. How long does the transfer alone take at 200 KB per second?

<details> <summary>Show answer</summary>

2.4 seconds. The total is 30 + 60 + 300 + 90 = 480 KB, and 480 / 200 = 2.4 s. Round trips come on top of that, so the real time to the main image is longer.

</details>

2. The product page makes three API calls: product details, then reviews (which need an id from the details), then recommendations (which need the category from the reviews). On the slow profile, how much waiting do these calls add, and how much if the server returned all three in one response?

<details> <summary>Show answer</summary>

450 ms for three dependent calls, and 150 ms for one. Each call waits for the previous response, so the three round trips add 3 x 150 ms, not counting server time. One combined response needs one round trip, which saves 300 ms on this profile.

</details>

3. A search box runs a 7 ms filter on every key press on a desktop. Will it fit in one frame on Basket's target phone at 60 Hz?

<details> <summary>Show answer</summary>

No. On a phone 4 times slower, 7 ms becomes about 28 ms. After 6 ms of browser work, a 60 Hz frame leaves about 10.7 ms, so the filter misses at least one frame on each key press. The work has to shrink, move off the main thread, or run less often.

</details>

4. A shopper scrolls through 2,000 products in a list that never removes rows. Each card has 20 nodes. How many nodes does the page hold, and why does it matter even if images far off screen are dropped?

<details> <summary>Show answer</summary>

40,000 nodes. The count is 2,000 x 20. The browser keeps every node in memory, and style and layout work over the list grows with the node count, so many changes to the page cost more as the list grows. Dropping decoded images lowers memory, but it does not remove a single node.

</details>

5. A live dashboard receives 50 updates per second, and each one costs 6 ms on the target phone. What share of the main thread does it use, and what are two ways to lower it?

<details> <summary>Show answer</summary>

30 percent of every second. The cost is 50 x 6 = 300 ms per 1,000 ms. The page can ask the server to send only the changes it shows. It can also collect updates and draw them together a few times per second. Then the cost depends on how often the page draws, not on how often messages arrive.

</details>

Reading Progress

0%


Vote for new content

On This Page

The Numbers to Start From

Page Weight

Requests and Round Trips

The Frame Budget

Memory for a Long List

Updates per Second

The Estimates in One Table

Using This in an Interview

Key Takeaways

Practice Questions