Loading...

HTML essentialsAdvanced

Performance & loading strategies

7 min lesson

Next
  • Performance means making the page load and respond quickly, especially on slow networks and phones.
  • A page has stages: HTML download, parsing, CSS, JavaScript and images; each adds time.
  • Loading attributes like defer, async and loading="lazy" let the browser schedule work better.
  • Minimizing, compressing (gzip or Brotli) and caching files on the server also reduce download time.
  • Think of performance as packing a suitcase: bring only what you need, and pack it so it is quick to open.
  • This builds on HTML security: both are production concerns that you set before you ship.
  • The example adds defer to a script, lazy loading to an image and preload to a font; you will see the same page but load faster.
  • Next, Common mistakes & debugging HTML fixes the issues performance work often exposes.
Keep in mind
  • Use defer for scripts that must wait for HTML, async for independent ones.
  • Add loading="lazy" to images below the fold.
  • Minify and compress CSS, JavaScript and HTML.
  • Measure before and after with DevTools; guesswork wastes time.
index.html
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Performance</title>
  <!-- Preload a font used above the fold -->
  <link rel="preload" href="font.woff2" as="font" type="font/woff2" crossorigin>
  <!-- defer: run the script after the HTML is parsed -->
  <script src="app.js" defer></script>
</head>
<body>
  <!-- Above-the-fold image: loads immediately -->
  <img src="hero.jpg" alt="Hero image" width="800" height="400">
  <!-- Lazy image: loads only when scrolled near -->
  <img src="photo.jpg" alt="A photo" loading="lazy" width="400" height="300">
</body>
</html>

What happens: The page shows the hero image immediately and loads the second photo only when the user scrolls near it. The script runs after the HTML is ready.

Breaking it down

defer

<script src="app.js" defer>

Downloads the script in parallel but runs it only after the HTML is parsed, so the DOM is ready.

async

<script src="analytics.js" async>

Downloads in parallel and runs as soon as ready. Order is not guaranteed, so use it for independent scripts.

loading=lazy

<img loading="lazy">

Tells the browser to delay loading until the image is near the viewport, saving bandwidth.

preload

<link rel="preload" as="font">

Starts downloading a key resource early. Use it only for truly critical files.

Loading attributes

AttributeUse for
deferScripts that need parsed HTML
asyncIndependent third-party scripts
loading=lazyImages and iframes below the fold
preloadCritical fonts and styles
preconnectEarly connection to a domain

A helpful analogy

Loading a page is like moving into a house: bring the essentials first (hero image and text), send the rest later (lazy images), and turn on the lights only when needed (defer scripts). Packing light makes the move faster.

Nesting rules

  • 1.Do not put defer and async together; pick one.
  • 2.Preload only resources you truly need on the first screen; too many preloads slow the page.

Common mistakes

Using async on a script that depends on the DOM

Avoid

<script src="app.js" async> with querySelector

Use instead

<script src="app.js" defer>

async may run before the HTML is parsed, so querySelector finds nothing. defer waits for the HTML.

Lazy-loading the hero image

Avoid

<img src="hero.jpg" loading="lazy">

Use instead

<img src="hero.jpg">

The first image is what users see. Lazy loading delays it and hurts perceived speed.

Quick tips

  • •In DevTools, the Network panel shows the load order; check that critical files come first.
  • •Compress images with modern formats like WebP or AVIF; large JPEGs are often the biggest cost.
Previous
Next