JavaScript Execution Cost — Performance

JavaScript costs four things: download, parse, compile and execute. The last three need CPU work, mostly on the main thread, while an image is only downloaded…

The shop page is already on your phone's screen. You tap "Add to cart", and nothing happens. The button isn't broken. The browser is busy running JavaScript, and your tap has to wait in line.

JavaScript is expensive for more than its size. After the download, the browser still has to read the code, turn it into instructions and then run it, and most of that work happens on the main thread: the one lane the browser also uses to respond to clicks and draw the screen. Picture a food stall with one cook. An image is a ready-made cake: unwrap it and put it in the display. JavaScript is a recipe: the cook has to read and understand it first, and only then cook. While the cook is cooking, new orders wait in line.

Four stages: download, parse, compile, execute

  1. Download: the file travels over the network. This stage does not use the main thread.
  2. Parse: the browser reads the code's text and works out its structure.
  3. Compile: the parsed code is turned into instructions the JavaScript engine can run.
  4. Execute: the code runs: it creates variables, calls functions, changes the DOM.

Parse, compile and execute need CPU work, and most of it runs on the main thread. An image only needs a download and then a decode (turning the file into pixels), and modern browsers can decode off the main thread. That is why 200 KB of JavaScript costs far more than a 200 KB image.

Simulated numbers from the Lab on this page, on a 1.6 Mbps network (slow 4G, the same as Lighthouse):

Stage 200 KB JS, laptop 200 KB JS, mid-range phone 200 KB image, mid-range phone
Download 1,000 ms 1,000 ms 1,000 ms
Parse and compile 20 ms 80 ms none
Execute 80 ms 320 ms none
Decode none none 40 ms
CPU work 100 ms 400 ms 40 ms

The download is the same because the network is the same. What differs is the CPU work. A mid-range phone runs JavaScript several times slower than a laptop. The Lab uses 4 times, the same CPU slowdown Lighthouse uses. How long execute takes also depends on what the code does, not only on its size.

There is only one main thread

The main thread is the browser's one lane for running JavaScript, working out layout, painting and responding to input. Its work is split into tasks: a piece of work that has to finish before the browser may do anything else. A task longer than 50 ms is called a long task.

On a mid-range phone, those 200 KB become one 400 ms task: parse and compile from 0 to 80 ms, then execute from 80 to 400 ms without a break.

Time Main thread Your click
0 ms starts parse and compile
80 ms starts execute
150 ms still executing you click, the click joins the queue
400 ms task done the click handler starts
410 ms idle the handler is done (10 ms)

Your click waits from the 150 ms mark to the 400 ms mark: 250 ms. All that time the button feels dead, just like the cart button at the start. From the Core Web Vitals lesson you know INP, which is good at 200 ms or less. One long task like this is enough to make INP bad.

Splitting a long task

The total work cannot disappear, but it can be split into small pieces. Between the pieces, the code yields to the browser, so a click waiting in line can be handled first.

function yieldToBrowser() {
  return new Promise((resolve) => setTimeout(resolve, 0));
}

async function startApp(products) {
  let start = performance.now();
  for (const item of products) {
    renderCard(item);
    if (performance.now() - start > 25) {
      await yieldToBrowser(); // a click waiting in line is handled here
      start = performance.now();
    }
  }
}

With 25 ms pieces, the 200 KB on a mid-range phone look like this: a first task from 0 to 105 ms (80 ms of compile plus the first piece), then 25 ms pieces up to the 400 ms mark. A click at 150 ms lands in the piece from 130 to 155 ms, so it waits only 5 ms. The total work is still about 400 ms, even a little longer, but the page feels alive.

The first task is still 105 ms, because you cannot split parse and compile yourself.

Send less JavaScript

The strongest way to cut parse, compile and execute is not to send the code at all. In the Lab, 50 KB on a mid-range phone needs only 20 ms of compile, so once the work is split there is no long task at all. How to do that comes in later lessons, such as code splitting (loading code per page). From the critical rendering path lesson you already have defer, so a script does not hold up the HTML.

⚠️ Why does the button still feel dead?

TL;DR: JavaScript costs four things: download, parse, compile and execute. The last three need CPU work, mostly on the main thread, while an image is only downloaded and decoded. There is only one main thread: a task longer than 50 ms is a long task, and a click that arrives in the middle of one waits until it ends (a click at 150 ms in a 400 ms task waits 250 ms). A mid-range phone runs JavaScript several times slower than a laptop, so measure there. Split long work into small pieces with an await on setTimeout so a click can get in, and send less JavaScript, because you cannot split parse and compile.

⚡ Same size, different cost

Guess first: a JavaScript file and an image are both 200 KB. Why does the JavaScript cost more?

JavaScript has to be parsed, compiled, then run, mostly on the main thread. An image is only decoded and then shown. On a mid-range phone: 400 ms of CPU work for the JavaScript, 40 ms for the image.

Meaning: The download takes just as long because the network is the same. What differs is the CPU work, and a mid-range phone is several times slower than a laptop.

⚡ There is only one main thread

Meaning: A task longer than 50 ms is a long task. A click that arrives in the middle of one has to wait: a click at 150 ms in a 400 ms task waits 250 ms, and INP turns bad.

⚡ Split it, then give the browser a turn

Write:

if (performance.now() - start > 25) {
  await yieldToBrowser();
  start = performance.now();
}

Meaning: Each time a piece passes 25 ms, the loop pauses briefly through setTimeout(resolve, 0), so a click waiting in line is handled first. The total work stays the same, but the page feels alive. You can't split parse and compile, so send less JavaScript.

What you will learn