← Web Performance (Core Web Vitals) learning path
Web Performance (Core Web Vitals) · Module 4 of 8

JavaScript cost

Every kilobyte of script must be downloaded, parsed and run.

📖 Key ideas

  1. JavaScript is expensive: on slow phones, parsing and running it can take longer than downloading it.
  2. Code splitting loads only the code a page needs, and loads the rest later.
  3. Use defer or async on scripts so they do not block the first render.
  4. Remove unused libraries and check bundle size with a bundle analyser.
  5. Break long tasks (over 50 ms) into smaller pieces to keep the page responsive.
💡 Real-life example: A heavy JavaScript bundle is like a long instruction manual handed to a waiter before serving: the food (your page) waits until reading is done.
BundleCombined JavaScript file built for the browser
Code splittingBreaking a bundle into parts loaded when needed
deferScript attribute that runs it after HTML is parsed
Long taskMain-thread work longer than 50 milliseconds