Why is the page so slow?
You open a shop page on your phone. You wait. And wait. The page is slow.
The reason is often one huge photo. It is 12 MB, and the screen shows it very small.
Developers choose pictures, sounds and videos with care. This lesson shows you how.
Key words
| Word | What it means | Example |
|---|---|---|
| Asset | A file your software uses. | logo.png |
| File type | The format of a file. The ending of its name often shows it. | .jpg, .mp3 |
| File size | How much space a file takes. | 4 MB |
| Compression | Making a file smaller. | |
| Lossy | Compression that removes detail for good. | JPEG, MP3 |
| Lossless | Compression that keeps every detail. | PNG, ZIP |
| Streaming | Playing a sound or video while it downloads. | |
| Metadata | Facts about a file. | date, author, size |
| Bandwidth | How much data a connection can carry each second. | 10 MB per second |
The cells students fill in are left blank in this preview.
Small and large: KB and MB
| Unit | About how big | Example |
|---|---|---|
| 1 KB (kilobyte) | A short text file | A page of plain text |
| 1 MB (megabyte) | 1,000 KB | One web photo, or a minute of music |
| 1 GB (gigabyte) | 1,000 MB | A film, or a game update |
The cells students fill in are left blank in this preview.
Quick check: compression
Have a go
Quick check: compression
3 questionsCommon file types
| Asset | Type | Good for | Watch out |
|---|---|---|---|
| Graphic | JPEG (.jpg) | Photos. Small files, lossy | Blurry edges on text and logos |
| Graphic | PNG (.png) | Logos, icons, clear edges. Lossless | Large files for photos |
| Graphic | SVG (.svg) | Shapes and logos that scale to any size | Not for photos |
| Audio | MP3 (.mp3) | Music. Small, lossy | Some detail is lost |
| Audio | WAV (.wav) | Short sound effects, full quality | Very large files |
| Video | MP4 (.mp4) | Almost every device plays it | Needs careful encoding (see below) |
| Code | JavaScript library (.js) | Ready-made code that adds a feature | Every library adds KB and has rules for how you may use it |
The cells students fill in are left blank in this preview.
Match the job to the type
Have a go
Loading…
Your answers are marked here and are not kept. A college account keeps every student’s marks.
Audio and video: stream or not?
A long video is too big to download first. So you stream it. The player shows the start while the rest still arrives.
To encode means to change a recording into a file format and a size that suits the device. You encode a video once, then stream it.
You can also embed a video. This means you put a player on your page that plays a video stored somewhere else, such as YouTube.
Worked example: size and time
| Question | Step | Answer |
|---|---|---|
| 50 photos, 4 MB each. How much space? | 4 MB times 50 = 200 MB | 200 MB |
| A 100 MB file on a connection of 10 MB per second. How long? | 100 MB divided by 10 MB per second | 10 seconds |
| The same file on 5 MB per second? | 100 divided by 5 | 20 seconds |
| Two files, 3 MB and 7 MB. Total? | 3 + 7 | 10 MB |
The cells students fill in are left blank in this preview.
Ask six questions about every asset
Important
What quality do users need? How much bandwidth do they have? How much storage is there? Which device will show it? Which file type does that device play? Is there metadata to keep, such as the author and who owns it?
Work out the sums in Python
photo_mb = 4
count = 50
print("Space needed:", photo_mb * count, "MB")
file_mb = 100
speed = 10
print("Seconds:", file_mb / speed)
# Change the speed to 5 and run again.
Make a free account to save your work and track your progress.
Check a page against its budget
A web team has a budget for each page. Write four small functions. The first adds up the sizes of the assets in KB. The second works out the download time. The third says if the page is inside the budget. The fourth finds the largest asset, the first one to shrink. Press Run Tests.
def page_weight(assets):
# assets is a list of pairs: (name, size in KB), like [("logo.png", 20), ("photo.jpg", 400)].
# Give back the total size in KB.
return None
def load_seconds(total_kb, speed_kb_per_second):
# Seconds to download total_kb at the given speed. 1000 KB at 500 KB per second is 2 seconds.
return None
def fits_budget(assets, limit_kb):
# True if the page weight is the limit or less. Otherwise False.
return None
def biggest_asset(assets):
# Give back the NAME of the largest asset. An empty list gives back None.
return None
Make a free account to save your work, or press Start lab to work through the whole lesson.
Sizes and sums
Have a go
Sizes and sums
4 questionsExit quiz
Have a go